今天做“运算符重载”时在类内定义了析构函数,结果执行时发现控制台什么都没输出。删掉析构函数则可以正常运行。
代码如下:
#include <iostream>
using namespace std;
class Time{
private:
int mintue;
int second;
public:
Time(){mintue=0;second=0;};
Time(int m,int s);
friend Time operator+(Time&,Time&);
~Time(){delete(this);};
int getMintue(){return this->mintue;};
int getSecond(){return this->second;};
};
Time::Time(int m,int s){
mintue=m;
second=s;
}
Time operator+(Time & t1,Time & t2){
Time t;
t.mintue=t1.mintue+t2.mintue;
t.second=t1.second+t2.second;
if(t.second>=60){
t.second-=60;
t.mintue++;
}
return t;
}
int main(){
Time t,t_1(2,45),t_2(7,15);
t=t_1+t_2;
cout<<"(mintue:second)->"<<t.getMintue()<<":"<<t.getSecond()<<endl;
return 0;
}
运行结果为空,什么都没有输出,也没有报错
我认为导致这个结果的原因可能是** 这个析构函数把我类中函数内定义的对象给销毁了,导致传了一个空参返回输出**
有没有能创建析构函数后正常运行的修改意见呢?还是说在这种需要调用到类内部函数有对象的类是不能使用含有“delete”的析构函数呢?或者其他可能?
希望能得到大家的指点 QWQ