读书人

关于VC中const_cast修改变量的有关问题

发布时间: 2012-06-25 18:37:39 作者: rapoo

关于VC中const_cast修改变量的问题
测试语句如下:
const int j = 10;
const int* i = &j;
cout<<"the value j now is :"<<j<<endl;

*(const_cast<int*>(i)) = 200; //通过指针修改
cout<<"the value *i now is :"<<*i<<endl;
cout<<"the value j now is :"<<j<<endl;

const int& k = j;
const_cast<int&>(k) = 300; //通过引用修改
cout<<"the value &k now is :"<<k<<endl;
cout<<"the value *i now is :"<<*i<<endl;
cout<<"the value j now is :"<<j<<endl;

现在的问题是:
为什么j的值最后还是10呢?

[解决办法]
const_cast 后 对const本尊是否有修改权?c++标准没有定义,vc++ 不允许,这也是比较合理的,否则,const变量就可以随便改了,还要什么const呢?

[解决办法]

探讨

引用:
你对const_cast的理解是错误的


什么地方理解有问题?请教~!

读书人网 >VC

热点推荐