c++ - constructor\destructor 或者对OOP的理解

标签 c++ constructor operator-overloading destructor

我试图在我创建的矩阵类上对 + 和 = 运算符使用运算符重载。要么是构造函数或析构函数导致了问题,要么都不是(尽管我将它们中的每一个都变灰了,并且代码似乎可以工作)。有人可以帮我理解是什么导致了这种奇怪的行为。当我尝试创建 3 个矩阵 a b 和 c 然后尝试 a = b+c;它只是失败了。

header file
#ifndef MATRIX_H;
#define MATRIX_H;
using namespace std;


enter code here

class matrix
{
friend ostream& operator<< (ostream&, matrix &);
public:
    matrix();
    matrix(int,int); //constructor
    matrix(const matrix&);//copy constructor
    ~matrix();
    int getRow();
    int getCol();
    void setRow(int);
    void setCol(int);
    class arr1D{                //proxy class to allow the use of [][]       operator
        public:
        arr1D(int* a):temp(a){}
        int &operator[](int a){
            return temp[a];
        }
        int *temp;
    };
    arr1D operator[](int a){
    return arr1D(arr2[a]);
    }
    matrix& operator=(const matrix& );

    matrix& operator+(const matrix& );

protected:
private:
    int row , col;
    int **arr2;

   };

   #endif // MATRIX_H

enter code here
cpp file
#include <iostream>
#include "matrix.h"

using namespace std;

matrix::matrix()
{
setCol(0);
setRow(0);
**arr2=0;
}


matrix::matrix(int x, int y) //matrix constructor creates x*y matrix and                 initializes to 0's
{
setCol(y);
setRow(x);
arr2 = new int * [getRow()];
if (arr2) {
    for (int i = 0; i < getRow(); i++) {
        arr2[i] = new int [getCol()];

    };
};
for (int i=0; i<getRow();i++){
    for (int j=0;j<getCol();j++){
        arr2[i][j]=0;
    };
};
}

matrix::matrix(const matrix &m){ //defines the copying constructor
 row=m.row;
 col=m.col;
arr2 = new int*[row];
for (int i=0; i<row; i++){
    arr2[i] = new int[col];
    }
for (int i=0; i<row; i++){
    for (int j=0; j<col; j++){
        arr2[i][j] = m.arr2[i][j];
    }
 }

}

 matrix::~matrix(){ //defines the destructor
 for (int i=0; i<row; i++){
    delete[] arr2[i];
    }
delete[] arr2;
}


int matrix::getRow(){ //getter for row
return row;
}

int matrix::getCol(){ // getter for col
return col;
}

void matrix::setRow(int x){ //setter for row
row=x;
}

void matrix::setCol(int x){ //setter for col
col=x;
 }


ostream& operator<< (ostream& output, matrix& a){
     int i,j;
     for (i=0; i < a.getRow() ; i++){
        for (j=0; j< a.getCol() ; j++){

            output << " " <<a.arr2[i][j];

        };
         output << "\n";
     };
return output;
}

matrix& matrix::operator=(const matrix& right)
{
if (this == &right) {     // Same object?
  return *this;
}
row = right.row;
col = right.col;
for (int i=0; i<row; i++)
{
  for (int j=0; j<col; j++){
    arr2[i][j]=right.arr2[i][j];
  }
}
return *this ;
}

matrix& matrix::operator+(const matrix& right)
{
int row=right.row;
int col=right.col;
matrix result(row,col);
for (int i = 0; i < row; i++){
    for (int j = 0; j < col; j++){
            //cout<<"arr2[i][j]="<<arr2[i][j]<<endl;
            //cout<<"right.arr2[i][j]="<<right.arr2[i][j]<<endl;
        result.arr2[i][j]=(arr2[i][j] + right.arr2[i][j]);
            //cout<<"result.arr2[i][j]="<<result.arr2[i][j]<<endl;
    };
 };
 return result;
 }

最佳答案

首先,正如其他答案所指出的那样,您将返回对 operator + 中临时对象的引用。这是未定义的行为。

但是,与其以这种方式编写 operator +,不如编写 operator +=,然后依次编写 operator +operator += 方面。由于程序员会期望 += 除了 + 之外还适用于 matrix,因此省略 + 是没有意义的=

对于 operator +=,在这种情况下您将返回对当前对象的引用。

所以我们需要做的就是将operator +中的代码移动到operator +=:

#include <exception>
//...
matrix& matrix::operator+=(const matrix& right)
{
    if(row != right.row || col != right.col)
       throw std::logic_error("Matrix not the same size");

    for (int i = 0; i < right.row; i++)
    {
        for (int j = 0; j < right.col; j++)
             arr2[i][j] += right.arr2[i][j]);
    }
    return *this;
 }

请注意,我们返回了对当前矩阵的引用,因为 += 修改了当前矩阵。另请注意,我们在发送到 += 的非法矩阵上抛出异常。这个 IMO 比在出错时返回合法的 matrix 更有意义。如果矩阵大小不同,代码不应尝试返回矩阵。

现在operator +可以写成+=:

   matrix matrix::operator+(const matrix& right)
   {
      return matrix(*this) += right;
   }

信不信由你,就是这样。我们所做的只是创建一个临时矩阵并使用传入的参数调用 += 。正如我们预期的那样,我们将此结果作为一个全新的矩阵返回。

另一个问题是赋值运算符。鉴于您已经编写了复制构造函数和析构函数,并且复制构造函数无需使用赋值运算符即可工作,那么 copy / swap idiom可用于实现赋值运算符。

   #include <algorithm>
   //...
   matrix& matrix::operator=(const matrix& right)
   {
      matrix temp(right);
      std::swap(temp.arr2, arr2);
      std::swap(temp.row, row);
      std::swap(temp.col. col);
      return *this;
   }

我们在这里所做的只是创建一个传入对象的临时矩阵,并将其内容与当前对象的内容交换。当临时文件在返回时消失,临时文件会销毁被换出的旧内容。

这个方法的优点是不仅实现起来非常简单(只是一堆调用std::swap),而且是异常安全的。如果在创建临时矩阵时出现问题,则会抛出 std::bad_alloc 异常,而不会弄乱或更改 this 的任何成员。使用上述技术解决了另一个答案的问题,即在分配新内存之前先取消分配内存。

关于c++ - constructor\destructor 或者对OOP的理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41106008/

相关文章:

python - 在不隐藏构造函数、可选参数或功能的情况下继承时避免使用长构造函数

c++ - Allegro 中的相等运算符重载

c++ - 不同编译器之间的名称查找不一致

c++ - 重载函数中的歧义 : how is it resolved?

c++ - 如何以及何时使用 Q_DECLARE_METATYPE

Java 复制构造函数 ArrayLists

c++ - 在静态函数中返回一个对象而不是构建它有什么好处?

c++ - 用不同的对象实例化 C++ Vector

c++ - 交替递增顺序反向堆叠

c++ - CMake - 使用 find_package 中的变量链接用于调试和发布版本的不同库