I have a function returning a local object:
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();
output is:
construct
copy
destroy
destroy
I suppose there are only construct and destroy of “tmp”, so why there is copy and another destroy? where is the copied object?
Because it is
1) created:
AT tmpinside funcAt2) copied:
return tmp;and this is because the function returns a copy:AT funcAt()3) destroy – the first tmp object, and the returned copy
Hint: note the
copyin the output 🙂