测试语句如下:
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呢?
------解决方案--------------------------------------------------------