赋值和添加运算符重载(2 个表和 int 的串联)

Assigment and adding operator overloading (concatenation of 2 tables and int)

我正在尝试连接两个数组并在最后连接 int,例如:result = arg + arg + 2; 我在 + 运算符重载时得到 "read access violation"。 我在下面的评论中写下了错误和警告。

我的代码:

主要:

#include <iostream>
#include <string>
#include "CTable.h"
int main() {

    CTable c_tab1, c_tab0;
    c_tab0.SetNewSize(3);
    c_tab1.SetNewSize(2);

    c_tab0.SetValueAt(0, 22);
    c_tab0.SetValueAt(1, 23);
    c_tab0.SetValueAt(2, 24);
    c_tab0.Print();

    c_tab1.SetValueAt(0, 31);
    c_tab1.SetValueAt(1, 32);
    c_tab1.Print();

    CTable c_tab3 = (c_tab0 + c_tab1 + 111);
    c_tab3.Print();

    return 0;
}

Class C表:

#include <iostream>
#include <string>

using namespace std;

class CTable {

public:
    CTable();
    CTable(string sName, int iTableLen);
    CTable(const CTable& pcOther);
    CTable* pcClone();
    ~CTable();

    void ShowName();
    void ShowSize();
    void SetName(string sName);
    bool SetNewSize(int iTableLen);
    void SetValueAt(int iOffset, int iNewVal);
    void Print();
    CTable& operator+(const CTable& pcNewTable);
    CTable operator+(int iNewVal) const;
    CTable& operator=(const CTable& pcNewVal) {
        if (this != &pcNewVal) {
        for (int i = 0; i < i_size; i++) {
            this->piTable[i] = pcNewVal.piTable[i];
        }
    }
        return *this;
    }

private:
    string s_name;
    int i_size;
    int* piTable;

    const int SIZE = 10;
    const string NAME = "Name";
};
#include <iostream>
#include <string>
#include "CTable.h"
#include <algorithm>

using namespace std;

CTable::CTable() {
    s_name = NAME;
    cout << "bezp: " << s_name << endl;
    piTable = new int[SIZE];
    i_size = SIZE;
}

CTable::CTable(string sName, int iTableLen) {
    s_name = sName;
    cout << "parametr: " << sName << endl;
    piTable = new int[iTableLen];
    i_size = iTableLen;
}

CTable::CTable(const CTable& pcOther) {
    s_name = pcOther.s_name + "copied";
    piTable = new int[pcOther.i_size];
    i_size = pcOther.i_size;
    for (int i = 0; i < pcOther.i_size; i++) { 
        piTable[i] = pcOther.piTable[i];
    }
}

CTable::~CTable() {
    delete[] piTable;
}

void CTable::SetName(string sName) {

    s_name = sName;
}

bool CTable::SetNewSize(int iTableLen) {

    if (iTableLen <= 0) {
        cout << "Length has to be greater than 0" << endl;
        return false;
    }

    int* pi_newTable = new int[iTableLen];
    for (int i = 0; i < iTableLen; i++) {
        pi_newTable[i] = piTable[i];
    }
    delete this->piTable;
    this->i_size = iTableLen;
    this->piTable = pi_newTable;

    return true;
}

CTable* CTable::pcClone() {
    CTable* ct = new CTable(s_name, i_size); 
    return ct;
}

void CTable::ShowName() {
    cout << "Name: " << s_name << endl;
}

void CTable::ShowSize() {
    cout << "Size: " << i_size << endl;
}

void CTable::SetValueAt(int iOffset, int iNewVal) {
    if (iOffset >= this->i_size) {
        return;
    }
    piTable[iOffset] = iNewVal;
}

void CTable::Print() {
    for (int i = 0; i < i_size; i++) {
        cout << piTable[i] << " ";
    }
    cout << endl;
}

CTable& CTable::operator+(const CTable& pcNewTable) {
    CTable result("new_int", this->i_size);

    result.i_size = (i_size + pcNewTable.i_size);
    result.piTable = new int[i_size + pcNewTable.i_size];

    for (int i = 0; i < i_size; i++) {
        result.piTable[i] = piTable[i];
    }

    for (int i = 0; i < (pcNewTable.i_size); i++) {
        result.piTable[i+i_size] = pcNewTable.piTable[i];
    }
    return result; //Warning C4172 returning address of local variable or temporary: result
}

CTable CTable::operator+(int iNewVal) const {
    CTable result("new_int", this->i_size);
    result.i_size = (i_size + 1);
    result.piTable = new int[i_size + 1];

    for (int i = 0; i < i_size; i++) {
        result.piTable[i] = piTable[i]; //Exception thrown: read access violation. **this->piTable** was 0x1110122.
    }
    result.piTable[i_size + 1] = iNewVal;
    return result;
}

我应该纠正什么?我不确定赋值运算符重载,可以吗?

成员函数 SetNewSize 有未定义的行为。在这个循环中

int* pi_newTable = new int[iTableLen];
for (int i = 0; i < iTableLen; i++) {
    pi_newTable[i] = piTable[i];
}

它 1) 使用未初始化的值,因为数组未初始化,并且 2) iTableLen 可以大于 i_size 的当前值。您至少应该在构造函数中对数组进行零初始化。

复制赋值运算符aslo有未定义的行为,因为对象数组的元素数pcNewVal可以小于赋值对象数组的元素数。

第一个重载 operator + 也有未定义的行为。对于初学者,正如警告所说,运算符 returns 对本地对象结果的引用在退出运算符后将不存在。其次,存在内存泄漏,因为对象的数组被重新分配,并且构造函数中先前分配的内存没有被释放。

CTable result("new_int", this->i_size);

result.i_size = (i_size + pcNewTable.i_size);
result.piTable = new int[i_size + pcNewTable.i_size];
//...

第二个重载 operator + 也有未定义的行为。与前面的运算符一样,存在内存泄漏。

在此声明中

result.piTable[i_size + 1] = iNewVal;

分配的数组外有访问内存。应该有

result.piTable[i_size] = iNewVal;