int main()
{
int const a = 10;
int const * p = &a;
int *q =const_cast<int *> (p);
*q = 20;
cout << *q << "\n" << *p << "\n" << a<<endl;
system("pause");
}
结果
分析:变量a为啥是10?
因为编译器在编译时会对常量做优化,并不是真的到内存中去取值
解决办法:
加关键字volatile
int volatile const a = 10;
网友评论