eis*_*baw 15 c++ vector const-correctness
考虑以下:
class A {
public:
const int c; // must not be modified!
A(int _c)
: c(_c)
{
// Nothing here
}
A(const A& copy)
: c(copy.c)
{
// Nothing here
}
};
int main(int argc, char *argv[])
{
A foo(1337);
vector<A> vec;
vec.push_back(foo); // <-- compile error!
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显然,复制构造函数是不够的.我错过了什么?
编辑:Ofc.我无法在operator =()方法中更改this-> c,因此我没有看到如何使用operator =()(尽管std :: vector需要).
GMa*_*ckG 19
我不确定为什么没有人这么说,但正确的答案是放下const或存储A*在向量中(使用适当的智能指针).
您可以通过"复制"调用UB或不执行任何操作(因此不是副本)来为您的类提供可怕的语义,但为什么所有这些都会在UB和坏代码中跳舞?做到这一点你得到了const什么?(提示:没什么.)你的问题是概念性的:如果一个类有一个const成员,那么这个类就是const.从根本上说,不能分配const的对象.
只需使它成为非const 私有,并且不可变地暴露其价值.对于用户来说,这是等效的,不变的.它允许隐式生成的函数正常工作.
Pra*_*rav 15
STL容器元素必须是可复制构造且可分配1(您的类A不是).你需要超载operator =.
1:§23.1说The type of objects stored in these components must meet the requirements of CopyConstructible
types (20.1.3), and the additional requirements of Assignabletypes
编辑:
免责声明:我不确定以下代码是否100%安全.如果它调用UB或其他什么,请告诉我.
A& operator=(const A& assign)
{
*const_cast<int*> (&c)= assign.c;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
编辑2
我认为上面的代码片段调用Undefined Behavior,因为试图抛弃const限定变量的常量调用UB.
| 归档时间: |
|
| 查看次数: |
4454 次 |
| 最近记录: |