【发布时间】:2017-03-13 01:33:20
【问题描述】:
http://coliru.stacked-crooked.com/a/8356f09dff0c9308
#include <iostream>
struct A
{
A(int& var) : r(var) {}
int &r;
};
int main(int argc, char** argv)
{
int x = 23;
A a1(x); // why this line is fine?
A a2 = a1; // why this line is fine?
a2 = a1; // error: use of deleted function 'A& A::operator=(const A&)'
// note: 'A& A::operator=(const A&)' is implicitly deleted because the default definition would be ill-formed:
// error: non-static reference member 'int& A::r', can't use default assignment operator
return 0;
}
默认赋值运算符被删除。为什么仍然保留默认的复制构造函数?
【问题讨论】:
-
因为引用可以被构造(初始化/绑定),但不能重新分配(反弹)。
-
@songyuanyao:请在回答区回答!