为什么我的本地物体被毁了两次

dem*_*xSH 2 c++ return-value

我有一个返回本地对象的函数:

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();
Run Code Online (Sandbox Code Playgroud)

输出是:

construct
copy
destroy
destroy
Run Code Online (Sandbox Code Playgroud)

我想只有"tmp"的构造和破坏,为什么有副本和另一个毁灭?复制对象在哪里?

and*_*ski 8

让我们充实一点:

AT funcAt()
{
    AT tmp;           [1]
    return tmp;       [2]
}                     [3]
...
funcAt();             [4]
Run Code Online (Sandbox Code Playgroud)

[1]在tmp
[2 ]中创建一个AT对象将tmp复制为返回值
[3] destroy tmp
[4]销毁返回值,因为它没有被使用