微软的BUG,请来看看!
可能不是BUG,但是我觉得很奇怪,有谁能告诉我为什么?
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int a(9);
const int * p;
p=const_cast<int *>(&a);
cout << "a=" << a << " before modify" << endl;
cout << "*p=" << *p << " before modify" << endl;
cout << "address of a=" << &a << " before modify" << endl;
cout << "p=" << p << " before modify" << endl << endl;
*const_cast<int *>(&a)=111;
cout << "a=" << a << " before modify" << endl;
cout << "*p=" << *p << " before modify" << endl;
cout << "address of a=" << &a << " before modify" << endl;
cout << "p=" << p << " before modify" << endl;
return 0;
}
请注意,修改后,*p 和 a 打印出来的值竟然不一样,如果在*const_cast<int *>(&a)=111;出设置一个断点,你可以发现在此句前后,调试里a的值是不一样的。
求教!谢谢
再问,如何给一个const的常量赋值?可以吗?