为什么在赋值运算符之后在此代码中调用复制构造函数?

时间:2015-11-20 05:09:46

标签: c++

如果我修改赋值opreator以便它返回一个对象A而不是对对象A的引用,那么就会发生一些有趣的事情。

每当调用赋值运算符时,都会在之后调用复制构造函数。这是为什么?

#include <iostream>
using namespace std;

class A {
private:
    static int id;
    int token;
public:
    A() { token = id++; cout << token << " ctor called\n";}
    A(const A& a) {token = id++; cout << token << " copy ctor called\n"; }
    A /*&*/operator=(const A &rhs) { cout << token << " assignment operator called\n"; return *this; }
};

int A::id = 0;

A test() {
    return A();
}

int main() {
    A a;
    cout << "STARTING\n";
    A b = a;
    cout << "TEST\n";
    b = a;
    cout << "START c";
    A *c = new A(a);
    cout << "END\n";
    b = a;
    cout << "ALMOST ENDING\n";
    A d(a);
    cout << "FINAL\n";
    A e = A();
    cout << "test()";
    test();

    delete c;
    return 0;
}

输出如下:

0 ctor called
STARTING
1 copy ctor called
TEST
1 assignment operator called
2 copy ctor called
START c3 copy ctor called
END
1 assignment operator called
4 copy ctor called
ALMOST ENDING
5 copy ctor called
FINAL
6 ctor called
test()7 ctor called

1 个答案:

答案 0 :(得分:5)

因为如果你没有返回对象的引用,它会复制。 正如@ M.M所说的最终test()调用一样,由于复制省略What are copy elision and return value optimization?

,副本没有出现