类析构函数导致重载+运算符出现问题



我不知道C++类(使用Visual Studio(有什么问题。Visual Studio没有给出任何期望的输出,只是说";以代码-103741819退出";。我使用原始指针创建了一个名为Complex的类,当没有调用args或参数化构造函数时,使用new int分配内存,当变量超出范围时,析构函数使用delete关键字释放内存。我面临的唯一问题是,我的+运算符重载导致了问题,我很确定这个问题与析构函数有关,当我删除析构函数代码时,+运算符工作得很好。或者当我不使用+运算符时,程序也可以正常工作。请帮我弄清楚代码。请不要对我说";这里不需要原始指针";,实际上,我被告知要这样做(只使用指针(。我被这个问题困扰了好几个小时
这是我的代码,请仔细阅读,包括+运算符重载代码和析构函数代码。

#include<iostream>
using namespace std;
class Complex {
private:
int *real;
int *complex;

public:
// some declarations
Complex();
Complex(int, int);
Complex(const Complex& source);
Complex operator+ (const Complex& rhs);
Complex& operator= (const Complex& rhs);
void disp() {
cout << "(" << *real << "," << *complex << ")" << endl;
}
// destructor
~Complex() {
delete real;
real = nullptr;
delete complex;
complex = nullptr;
}

};
// no-args constructor
Complex::Complex() {
real = new int;
*real = 0;
complex = new int;
*complex = 0;
}
// parameterized constructor
Complex::Complex(int x, int y) : Complex() {
*real = x;
*complex = y;
}
//copy constructor
Complex::Complex(const Complex& source) {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
// overloading + operator
Complex Complex::operator+ (const Complex &rhs) {
int a, b;
a = *(this->real) + *(rhs.real);
b = *(this->complex) + *(rhs.complex);
Complex temp(a,b);
return temp;
}
// overloading = operator
Complex& Complex::operator= (const Complex& rhs) {

*(this->real) = *(rhs.real);
*(this->complex) = *(rhs.complex);
return *this;
}
int main() {
Complex n1(5,-9);
Complex n2(5,-1);
Complex n3;
n3=n1 + n2;
n3.disp();

return 0;
}

您在复制构造函数中没有分配任何内存,因此您的分配发生在未初始化的内存上。

Complex::Complex(const Complex& source) {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}

如果我把它改成这个:

Complex::Complex(const Complex& source) : Complex() {
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}

您的程序输出(10,-10)

编辑:评论中的问题。

我在你的程序中添加了一些打印输出,以准确显示正在发生的事情:

#include<iostream>
using namespace std;
class Complex {
private:
int* real;
int* complex;
public:
// some declarations
Complex();
Complex(int, int);
Complex(const Complex& source);
Complex operator+ (const Complex& rhs);
Complex& operator= (const Complex& rhs);
void disp() {
cout << "(" << *real << "," << *complex << ")" << endl;
}
// destructor
~Complex() {
std::cout << "destructor" << std::endl;
delete real;
real = nullptr;
delete complex;
complex = nullptr;
}
};
// no-args constructor
Complex::Complex() {
std::cout << "constructor" << std::endl;
real = new int;
*real = 0;
complex = new int;
*complex = 0;
}
// parameterized constructor
Complex::Complex(int x, int y) : Complex() {
std::cout << "(x,y)constructor" << std::endl;
*real = x;
*complex = y;
}
//copy constructor
Complex::Complex(const Complex& source) : Complex() {
std::cout << "copy constructor" << std::endl;
*(this->real) = *(source.real);
*(this->complex) = *(source.complex);
}
// overloading + operator
Complex Complex::operator+ (const Complex& rhs) {
std::cout << "op+" << std::endl;
int a, b;
a = *(this->real) + *(rhs.real);
b = *(this->complex) + *(rhs.complex);
Complex temp(a, b);
return temp;
}
// overloading = operator
Complex& Complex::operator= (const Complex& rhs) {
std::cout << "op=" << std::endl;
*(this->real) = *(rhs.real);
*(this->complex) = *(rhs.complex);
return *this;
}
int main() {
Complex n1(5, -9);
Complex n2(5, -1);
Complex n3;
n3 = n1 + n2;
n3.disp();
return 0;
}

现在运行您的程序结果如下:

constructor
(x,y)constructor
constructor
(x,y)constructor
constructor
op+
constructor
(x,y)constructor
constructor
copy constructor
destructor
op=
destructor
(10,-10)
destructor
destructor
destructor

正如你所看到的;复制构造函数";在那里。具体地说,这一行:n3 = n1 + n2;导致打印输出:

op+               // n1 + n2
constructor       // base constructor from param constructor
(x,y)constructor  // constructing the return value: Complex temp(a, b);
constructor       // base constructor from copy constructor
copy constructor  // copying from temp to the return value
destructor        // destroying temp
op=               // assigning the return value to n3
destructor        // destroying the return value

请注意,这是在调试模式下编译的。如果我在发布模式下编译,输出会发生变化:

constructor
(x,y)constructor
constructor
(x,y)constructor
constructor
op+
constructor
(x,y)constructor
op=
destructor
(10,-10)
destructor
destructor
destructor

这里的关键点是,编译器通过认识到构建temp仅仅复制和销毁它是没有意义的,从而成功地优化了复制构造函数。但这只有在启用优化时才会发生。

我怀疑您的参数化构造函数是问题所在。。。在参数化构造函数中,您正在创建一个Complex类对象,该对象使用*real和*Complex指针,将其指向传入的整数(x和y(。没有分配内存,因此当程序结束时,会调用析构函数,并尝试释放n1和n2中从未动态分配的内存。

我已经几个月没有接触C++了,所以我可能错了。请随时查看并回复我您的结果。

最新更新