class Ctest
{
int m_a;
int b;
public:
Ctest(){}
Ctest(int a,int b):m_a(a),b(b){}
~Ctest(){
cout <<this->m_a <<"销毁局部变量" << endl;
}
int geta(){ return m_a; }
friend ostream &operator<< (ostream &out, const Ctest &ts)
{
out << ts.m_a <<"----"<< ts.b << endl;
return out;
}
int t(){ return m_a; }
Ctest &operator+(const Ctest &a)
{
Ctest temp;
temp.m_a= this->m_a + a.m_a;
return temp;
}
/*friend Ctest &operator+(const Ctest &a,const Ctest &b)
{
Ctest temp;
temp.m_a = b.m_a + a.m_a;
return temp;
}*/
};
main
Ctest a(222, 2), b(333, 3), c;
c = a + b;
cout << "c:" << c << endl;//c=555
cout << a.geta() << endl;//a=222
cout << c << endl;//c=555
为什么我重载+号运算符,返回一个局部变量的引用,却还是得到了正常的得数?不是局部变量销毁后,地址内的内容被删除,就应该乱码吗?