constructor\destructor 或者对OOP的理解
constructor\destructor or understanding of OOP
我正在尝试在我创建的矩阵 class 上对 + 和 = 运算符使用运算符重载。要么是构造函数或析构函数导致了问题,要么都不是(尽管我将它们中的每一个都变灰了,并且代码似乎可以工作)。有人可以帮我理解是什么导致了这种奇怪的行为。当我尝试创建 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;
}
首先,正如另一个答案所指出的,您正在 return引用 operator +
中的临时文件。那是未定义的行为。
但是不要以这种方式写 operator +
,你应该写 operator +=
,然后反过来,用 operator +=
写 operator +
.由于程序员会期望 +=
除了 +
之外还可以为 matrix
工作,所以省略 +=
是没有意义的。
对于 operator +=
,在这种情况下,您将 return 引用当前对象。
所以我们需要做的就是将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;
}
请注意,我们 return 是对当前矩阵的引用,因为 +=
修改了当前矩阵。另请注意,我们对发送到 +=
的非法矩阵抛出异常。这个 IMO 比 return 错误返回合法 matrix
更有意义。如果矩阵大小不一样,代码不应该尝试和 return 返回一个矩阵。
现在operator +
可以写成+=
:
matrix matrix::operator+(const matrix& right)
{
return matrix(*this) += right;
}
信不信由你,就是这样。我们所做的只是创建一个临时矩阵并使用传入的参数调用 +=
。我们return将这个结果作为一个全新的矩阵,正如我们所期望的那样。
另一个问题是赋值运算符。假设你已经编写了拷贝构造函数和析构函数,并且拷贝构造函数不需要使用赋值运算符就可以工作,那么可以使用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;
}
我们在这里所做的只是创建一个传入对象的临时矩阵,并将其内容与当前对象的内容交换。当临时文件在 return 处消失时,临时文件会销毁被换出的旧内容。
这个方法的优点是不仅实现起来非常简单(只是一堆对std::swap
的调用),而且是异常安全的。如果在创建临时矩阵时出现问题,则会抛出 std::bad_alloc
异常,而不会弄乱或更改 this
的任何成员。给出的另一个答案的问题,即在分配新内存之前先取消分配内存,已通过使用上述技术解决。
我正在尝试在我创建的矩阵 class 上对 + 和 = 运算符使用运算符重载。要么是构造函数或析构函数导致了问题,要么都不是(尽管我将它们中的每一个都变灰了,并且代码似乎可以工作)。有人可以帮我理解是什么导致了这种奇怪的行为。当我尝试创建 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;
}
首先,正如另一个答案所指出的,您正在 return引用 operator +
中的临时文件。那是未定义的行为。
但是不要以这种方式写 operator +
,你应该写 operator +=
,然后反过来,用 operator +=
写 operator +
.由于程序员会期望 +=
除了 +
之外还可以为 matrix
工作,所以省略 +=
是没有意义的。
对于 operator +=
,在这种情况下,您将 return 引用当前对象。
所以我们需要做的就是将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;
}
请注意,我们 return 是对当前矩阵的引用,因为 +=
修改了当前矩阵。另请注意,我们对发送到 +=
的非法矩阵抛出异常。这个 IMO 比 return 错误返回合法 matrix
更有意义。如果矩阵大小不一样,代码不应该尝试和 return 返回一个矩阵。
现在operator +
可以写成+=
:
matrix matrix::operator+(const matrix& right)
{
return matrix(*this) += right;
}
信不信由你,就是这样。我们所做的只是创建一个临时矩阵并使用传入的参数调用 +=
。我们return将这个结果作为一个全新的矩阵,正如我们所期望的那样。
另一个问题是赋值运算符。假设你已经编写了拷贝构造函数和析构函数,并且拷贝构造函数不需要使用赋值运算符就可以工作,那么可以使用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;
}
我们在这里所做的只是创建一个传入对象的临时矩阵,并将其内容与当前对象的内容交换。当临时文件在 return 处消失时,临时文件会销毁被换出的旧内容。
这个方法的优点是不仅实现起来非常简单(只是一堆对std::swap
的调用),而且是异常安全的。如果在创建临时矩阵时出现问题,则会抛出 std::bad_alloc
异常,而不会弄乱或更改 this
的任何成员。给出的另一个答案的问题,即在分配新内存之前先取消分配内存,已通过使用上述技术解决。