<分区>
当我试图弄清楚顶级const和const_cast时,我写了一些代码如下。
int main()
{
// m is a top-level const
const int m = 10;
// this is an undefined behavior according to *Primer c++*
// but I still compile and run this without warning.
int *q = const_cast<int *>(&m);
(*q)++;
//Then I print the address and the value
cout<< "the value of address "<< q <<" is "<< *q <<endl;
cout<< "the value of address "<< &m <<" is "<< m <<endl;
return 0;
}
打印结果让我一头雾水。
the value of address 0x7ffee6a43ad8 is 11
the value of address 0x7ffee6a43ad8 is 10
这是未定义的行为之一吗?当我执行“(*q)++”时到底发生了什么?
提前致谢