【发布时间】:2017-04-07 06:00:55
【问题描述】:
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(int a)
{
x = a;
cout << "CTOR CALLED";
}
A(A &t)
{
cout << "COPY CTOR CALLED";
}
void display()
{
cout << "Random stuff";
}
A operator = (A &d)
{
d.x = x;
cout << "Assignment operator called";
return *this;
}
};
int main()
{
A a(3), b(4);
a = b;
return 0;
}
这段代码的输出是:
CTOR 被召唤
呼叫中心
赋值运算符称为
复制 CTOR 调用
当我在 Visual Studio 中使用手表时,它显示 a 中的 x 的值甚至在调用重载赋值运算符之前就已更改。
那么为什么还要在这里调用复制构造函数呢?
【问题讨论】:
标签: c++ c++11 visual-c++ c++14