为什么函数返回时没有调用复制构造函数?

Ice*_*man 3 c++ copy-constructor

Line Line::operator =(Line ln) {
        cout << "Assignment operator\n";
        Line temp;
        temp.ptr = new int;
        *temp.ptr = *(ln.ptr);
        return temp;
    }
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,执行以下语句时不会调用复制构造函数:

return temp;
Run Code Online (Sandbox Code Playgroud)

由于返回是按值,为什么不调用复制构造函数?

谢谢

Ker*_* SB 6

这称为复制省略:允许在按值返回本地对象时复制,而是temp直接在调用者中构造本地对象(您的).即使复制构造函数具有副作用,也允许这样做.