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
}
线路1和线路2是否都表现出未定义行为?如果f1
和f2
是上面代码中列出的shared_ptr
类型,行为会有所不同吗?