use*_*155 8 c++ const shared-ptr const-cast
struct foo
{
const int A;
int B;
foo() : A(10), B(20) {}
};
void main()
{
foo f1;
const_cast<int&>(f1.A) = 4; //line 1
const foo f2;
const_cast<int&>(f2.B) = 4; //line 2
}
Run Code Online (Sandbox Code Playgroud)
第1行和第2行都表现出未定义的行为吗?请问行为有所不同,如果f1和f2是shared_ptr在上面的代码中列出的类型?
Bat*_*eba 15
行为都const_cast<int&>(f1.A) = 4 和 const_cast<int&>(f2.B) = 4是不确定的.
如果最初将对象定义为const,并且抛弃了const-ness 并尝试修改该对象,则行为未定义.
| 归档时间: |
|
| 查看次数: |
215 次 |
| 最近记录: |