我有一个返回本地对象的函数:
class AT
{
public:
AT() { cout<<"construct"<<endl; }
AT(const AT& at) { cout<<"copy"<<endl; }
~AT() { cout<<"destroy"<<endl; }
};
AT funcAt()
{
AT tmp;
return tmp;
}
...
funcAt();
输出是:
construct
copy
destroy
destroy
我想只有“tmp”的构造和销毁,那么为什么会有复制和另一个销毁呢?复制的对象在哪里?
让我们具体说明一下:
AT funcAt()
{
AT tmp; [1]
return tmp; [2]
} [3]
...
funcAt(); [4]
[1]在tmp中创建AT对象
[2] 将 tmp 复制到返回值中
[3] 摧毁tmp
[4] 销毁返回值,因为它没有被使用
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)