Ron*_*n_s 1 c++ constructor copy-constructor
考虑以下 :
class A
{
public:
int xx;
A(const A& other)
{
cout << "A cctor" << endl;
/* do some stuff */
}
A(int x) : xx(x) {} /* conversion constructor */
};
int main()
{
A a = 1;
A other = a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下(以及一般情况下)说CCtor从const转换为非const是正确的吗?
谢谢,罗恩
复制构造函数创建现有对象的新副本,该对象可能是const,也可能不是const.const A::A(const A& other)只是说我们不会other在副本中改变.事实上,如果你试图修改ctor中的其他内容,编译器会呻吟你.
创建的对象也可能是const,也可能不是const,具体取决于您如何声明它.