【问题标题】:Operator overloading with dynamic allocated matrix动态分配矩阵的运算符重载
【发布时间】:2014-07-02 20:19:58
【问题描述】:

我正在编写一些代码来自学 C++。

执行以下代码:

#include <iostream>

using namespace std;

class Matrix
{
    int m, n;
    public: 
    float** value;

    Matrix(int m, int n)
    {
        value = new float*[m];
        for (int i = 0; i < m; ++i)
            value[i] = new float[n];
        this->m = m; this->n = n;
    }

    ~Matrix()
    {
        for (int i = 0; i < m; ++i)
            delete[] value[i];      
        delete[] value;
    }

    Matrix(const Matrix& A)
    {
        m = A.getLength();
        n = A.getWidth();

        value = new float*[m];
        for (int i = 0; i < m; ++i)
            value[i] = new float[n];

        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                value[i][j] = A.value[i][j];    
    }

    int getLength(void) const
    {
        return m;
    }

    int getWidth(void) const
    {
        return n;
    }

    void print() const
    {
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
                cout << value[i][j] << "\t";
            cout << endl;
        }
    }


    Matrix operator + (const Matrix& B)
    {
        if (m != B.getLength() || n != B.getWidth())
            return Matrix(0, 0);

        Matrix C = Matrix(m, n);

        cout << value << endl;

        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                C.value[i][j] = value[i][j] + B.value[i][j];

        return C;
    }


    void operator = (const Matrix& A)
    {
        m = A.getLength();
        n = A.getWidth();

        value = new float*[m];
        for (int i = 0; i < m; ++i)
            value[i] = new float[n];

        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                value[i][j] = A.value[i][j];
    }

};


int main()
{
    Matrix A = Matrix(3, 3);


    A.value[0][0] = 1; A.value[0][1] = 2; A.value[0][2] = 3;
    A.value[1][0] = 4; A.value[1][1] = 5; A.value[1][2] = 6;
    A.value[2][0] = 7; A.value[2][1] = 8; A.value[2][2] = 9;

    Matrix B = Matrix (3, 3);
    B.value[0][0] = 1; B.value[0][1] = 2; B.value[0][2] = 3;
    B.value[1][0] = 4; B.value[1][1] = 5; B.value[1][2] = 6;
    B.value[2][0] = 7; B.value[2][1] = 8; B.value[2][2] = 9;


    Matrix C = A + B;
    cout << C.value << endl;
    C.print();

    return 0;    
}

在“矩阵 C = A + B”部分产生内存泄漏?不知道关联完成后返回的矩阵是否被销毁。如果是,有没有办法解决?

【问题讨论】:

  • Matrix C = A + B; 中没有泄漏,但您的赋值运算符已损坏,因此会泄漏:Matrix C(1,2); C = A+B;

标签: c++ class dynamic overloading operator-keyword


【解决方案1】:

在声明中

Matrix C = A + B;

没有泄漏。

尽管在您的代码中未使用复制赋值运算符,但您的复制赋值运算符不会释放先前分配给对象矩阵的内存。 复制赋值运算符与复制构造函数的不同之处在于对象已经创建并且具有分配的矩阵。还要检查是否有自赋值。

也应该声明为

Matrix & operator = (const Matrix& A)

至于operator +则应该声明为

Matrix operator + (const Matrix& B) const;

或作为

const Matrix operator + (const Matrix& B) const;

【讨论】:

  • 关于赋值运算符,为什么 D = C = A+B 不简单地复制值指针,因为那样我会返回一个对 *this 对象的引用?
  • @user3422072 A+ B 创建一个将被删除的临时对象。因此,您可能不会简单地复制指针的值。当临时对象依次被删除时,指针指向的内存将被删除。
猜你喜欢
  • 2015-07-29
  • 1970-01-01
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多