c++ - 我怎样才能返回一个对象,它自己在 C++ 中的堆上分配了空间?

标签 c++ function object return heap-memory

已解决

解决方案:提供拷贝构造函数,遵循三原则。

原始问题

我正在实现一个类,它是一个区间矩阵。 我的 Intervall 类看起来像这样(缩短):

class Intervall
{
friend std::ostream& operator<<(std::ostream& output, const Intervall& i);

public:
    Intervall();
    Intervall(double n);

private:
    double min_value;
    double max_value;
};

有了这个实现:

Intervall::Intervall(){
    min_value = 0;
    max_value = 0;
}

Intervall::Intervall(double n){
    min_value = n;
    max_value = n;
}

ostream& operator<<(ostream& output, const Intervall& i){
    output << "[" <<  i.min_value << ", " << i.max_value <<"]";
    return output;
}

我的 Matrix 类如下所示:

class IntervallRMatrix
{
friend std::ostream& operator<<(std::ostream& output, const IntervallRMatrix& irm);

public:
   IntervallRMatrix();
   ~IntervallRMatrix();

   void setIdentity();

   IntervallRMatrix clone();

private:
   void initialize();

   Intervall* data;
   Intervall** cells;
};

使用以下实现:

IntervallRMatrix::IntervallRMatrix(){
    initialize();
}

IntervallRMatrix::~IntervallRMatrix(){
    delete cells;
    delete data;
}

ostream& operator<<(ostream& output, const IntervallRMatrix& irm){
    output << "3 x 3" << endl;
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            output << irm.cells[i][j];
        }
        output << endl;
    }
    return output;
}

void IntervallRMatrix::setIdentity(){
    cells[0][0] = Intervall(1);
    cells[1][1] = Intervall(1);
    cells[2][2] = Intervall(1);
}

IntervallRMatrix IntervallRMatrix::clone(){
    IntervallRMatrix result;
    for(int i=0; i<3; i++){
        for(int j=0; j<3; j++){
            result.cells[i][j] = cells[i][j];
        }
    }
    return result;
}

void IntervallRMatrix::initialize(){
    data = new Intervall[9];
    for(int i=0; i<9; i++){
        data[i] = Intervall();
    }
    cells = new Intervall*[3];
    for(int i=0; i<3; i++){
        cells[i] = &data[3*i];
    }
}

这一切工作正常,直到我使用克隆功能。此代码产生内存错误:

int main()
{
    IntervallRMatrix m1;
    m1.setIdentity();
    cout << "m1: " << m1 << endl;

    IntervallRMatrix m2 = m1.clone();
    cout << "m2: " << m2 << endl;
    return 0;
}

第一个 cout 按预期工作。 第二个 cout 没有。当程序尝试读取 m2.cells[0][0] 时,错误就出现了。

如果我不在析构函数中删除单元格和数据,则不会发生此错误。

我以我的方式声明单元格和数据,因为我想重载方括号,以便我以后可以编写 Intervall = Matrix[i][j]。 我还想重载算术运算符,以便我可以编写 Matrix m3 = m2*m1。 事实上,我已经这样做了,那是第一次发生错误的地方。

现在,终于是我的问题了: 我如何实现一个返回 IntervallRMatrix 的函数,该函数不会导致内存错误,同时仍然能够在析构函数中释放分配的内存?

最佳答案

最好的解决方案是避免手动内存管理。使用适当的容器(例如 std::vector)或智能指针。

如果您必须进行手动管理,请确保您遵循Rule of Three .

关于c++ - 我怎样才能返回一个对象,它自己在 C++ 中的堆上分配了空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14629640/

相关文章:

c++ - 如何使用 enable_if 来限制整个类(class)

Python使用getattr调用带可变参数的函数

java - 如何避免在 AsyncTask 中传递过多的输入参数?

java - 什么是对象序列化?(Java)

c++ - 删除单向链表中的 m 个节点

c++ - STL 容器中的 const 指针

C素数崩溃于64901

c# - 使用图形的 Azure 函数我得到 "Could not load file or assembly ' System.Text.Json”

java - 使用 Collections 或我的函数计算 ArrayList 中对象的出现次数

c++ - 观察者模式——竞争条件