标签:sp c 代码 r bs c++ as 对象 return
下面的代码输出什么?为什么?
class Test
{
int m_i;
int m_j;
public:
Test(int v)
{
cout<<"Test(int v)"<<endl;
}
~Test()
{
cout<<"~Test()"<<endl;
}
};
Test Play(Test t)
{
return t;
}
int main()
{
Test t = Play(5);
}
用编译器检测 最后输出调用了两次析构函数?
我觉得是3次:
第一次 Play(5) 5隐式转化为一个Test 对象;调用一次构造函数
然后再return t;这个语句中 t采用默认的拷贝构造函数赋值给一个 临时对象比如temp;调用一次构造函数;
然后 Test t = Play(5);也就是 Test t = temp;这里也调用一次构造函数;
既然是三次构造函数,为什么不是三次析构函数,
看别人的博客说 Test t = temp;这里不会调用构造函数?不理解?
高手们谁可以解释一下?
标签:sp c 代码 r bs c++ as 对象 return
原文地址:http://blog.csdn.net/djb100316878/article/details/39801229