int main()
{
const int ia = 10;
int *pia = const_cast<int*>(&ia);
*pia = 5;
std::cout << &ia << "\t" << pia <<endl;
std::cout << ia << "\t" << *pia <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
0x28fef4 0x28fef4
10 5
Run Code Online (Sandbox Code Playgroud)
*pia并ia具有相同的地址,但它们具有不同的值.我的目的是用来const_cast修改一个常量值,但结果显示它不起作用.
有谁知道为什么?