在C++中,具有常量数据成员的类是否可以不具有复制赋值运算符



我正在设计一个类,该类应该有一个名为K的常量数据成员。我还希望这个类有一个复制赋值运算符,但编译器似乎会从任何具有const数据成员的类中隐式删除复制赋值运算符。此代码说明了基本问题:

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常量的保护吗?

在C++中,具有const数据成员的类可能具有复制构造函数

#include <iostream>
class A
{
private:
const int k_;
public:
A(int k) : k_(k) {}
A() = delete;
A(const A& other) : k_(other.k_) {}
int get_k() const { return k_; }
};
int main(int argc, char** argv)
{
A a1(5);
A a2(a1);
std::cout << "a1.k_ = " << a1.get_k() << "n";
std::cout << "a2.k_ = " << a2.get_k() << "n";
}

输出:

a1.k_ = 5
a2.k_ = 5

在C++中,具有const数据成员的类不能使用默认赋值运算符

class A
{
private:
const int k_;
public:
A(int k) : k_(k) {}
A() = delete;
A(const A& other) : k_(other.k_) {}
int get_k() const { return k_; }
};
int main(int argc, char** argv)
{
A a1(5);
A a2(0);
a2 = a1;
}

产生编译时错误:

const_copy_constructor.cpp: In function ‘int main(int, char**)’:
const_copy_constructor.cpp:18:10: error: use of deleted function ‘A& A::operator=(const A&)’
18 |     a2 = a1;
|          ^~
const_copy_constructor.cpp:1:7: note: ‘A& A::operator=(const A&)’ is implicitly deleted because the default definition would be ill-formed:
1 | class A
|       ^
const_copy_constructor.cpp:1:7: error: non-static const member ‘const int A::k_’, can’t use default assignment operator

在C++中,具有const数据成员的类可以使用非默认赋值运算符,只要您不尝试更改const数据成员即可,但如果某个底层成员无法修改,则最好仔细考虑使用此赋值运算符意味着什么

class A
{
private:
const int k_;
public:
A(int k) : k_(k) {}
A() = delete;
A(const A& other) : k_(other.k_) {}
A& operator=(A const& other)
{
// do nothing
return *this;
}
int get_k() const { return k_; }
};
int main(int argc, char** argv)
{
A a1(5);
A a2(0);
a2 = a1;
}

不会产生编译时错误。

从c++20开始,您现在可以通过定义自己的复制赋值运算符来复制具有一个或多个常量成员对象的对象。

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
// valid copy assignment operator in >= c++20
A& operator=(A const& in) {
if (this != &in)
{
std::destroy_at(this);
std::construct_at(this, in);
}
return *this;
}
};

这是由于basic.life中的更改而实现的,它允许透明地替换对象,包括那些包含常量子对象的对象,而不使用UB。

将const定义为静态const,这样就可以处理它了。

最新更新