我正在设计一个类,它应该有一个名为K
的常量数据成员,我还希望这个类有一个复制赋值操作符,但是编译器似乎隐式地从任何有常量数据成员的类中删除了复制赋值操作符,下面的代码说明了一个基本问题:
class A
{
private:
const int K;
public:
A(int k) : K(k) {} // constructor
A() = delete; // delete default constructor, since we have to set K at initialization
A & operator=(A const & in) { K = in.K; } // copy assignment operator that generates the error below
}
下面是它生成的错误:
constructor.cpp:13:35: error: cannot assign to non-static data member 'K' with const-
qualified type 'const int'
A & operator=(A const & in) { K = in.K; }
~ ^
constructor.cpp:6:13: note: non-static data member 'K' declared const here
const int K;
~~~~~~~~~~^
1 error generated.
我想我理解编译器为什么要这样做;要复制到的类的示例必须存在,然后才能被复制到,如果它是常量,我就不能在目标示例中赋值给K
,正如我在上面尝试做的那样。
我对这个问题的理解是正确的吗?如果是的话,有没有解决这个问题的方法?也就是说,我可以为我的类定义一个复制构造函数,同时仍然给予K
类似常量的保护吗?
2条答案
按热度按时间anauzrmj1#
const
数据成员的类可能具有复制构造函数。**输出:
const
数据成员的类不能使用默认赋值运算符。**产生编译时错误:
const
数据成员的类可以使用非默认赋值运算符,只要您不尝试更改const
数据成员即可,但您最好仔细考虑一下,如果无法修改某个基础成员,使用此赋值运算符意味着什么。**不会产生编译时错误。
tpxzln5u2#
从c++20开始,你现在可以通过定义你自己的复制赋值操作符来复制有一个或多个常量成员对象的对象。
这是由于www.example.com中的更改basic.life,允许透明替换对象,包括那些包含常量子对象的对象,w/o UB。