局部变量和临时变量的区别
有个程序如下,最后结果为:destructor:this=0x241ff2c,num=3
destructor:this=0x241ff5c,num=3
书上说第一个是fun2()的内部局部变量析构,第二个是fun2的返回临时对象析构
怎么理解呢?
#include<iostream>
using nameespace std;
class Test
{
public:
Test():num(0){}
Test(int number):num(number){}
~Test()
{
cout<<"destructor:this="<<this<<",num="<<num<<endl;
}
private:
int num;
};
Test fun2()
{
Test t(3);
return t;
}
int main(int argc,char* argv[])
{
fun2();
return 0;
}