0
class B
{
public:
B(int& a) :ref(a){}
B(B const&) = default;
int& ref;
};
void main()
{
int a1 = 1, a2 = 2;
B b1(a1), b2(b1), b3(a2);
b3 = b1;
}
如果编译器执行的隐式定义的拷贝赋值运算符是,隐式定义的拷贝赋值运算符
B& operator=(const B& rhs)
{
this->ref = rhs.ref;
return *this;
}
为什么不能引用生成?绑定到变量a的初始别名不受影响,因为在复制赋值运算符中,引用变量ref的数值会被更改。
http://stackoverflow.com/questions/26946201/why-do-reference-type-members-cause-implicitly-declared-copy-assignment-operator的DUP? –
'main'必须返回'int',而不是'void'。 – melpomene