我知道使用const_cast通常是坏主意,但我正在玩它,我遇到了一个奇怪的行为,其中:
两个指针具有相同的地址值,但在取消引用时,给出不同的数据值.
有没有人对此有解释?
码
#include <iostream>
int main()
{
const int M = 10;
int* MPtr = const_cast<int*>(&M);
(*MPtr)++;
std::cout << "MPtr = " << MPtr << " (*MPtr) = " << (*MPtr) << std::endl;
std::cout << " &M = " << &M << " M = " << M << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
产量
MPtr = 0x7fff9b4b6ce0 (*MPtr) = 11
&M = 0x7fff9b4b6ce0 M = 10
Run Code Online (Sandbox Code Playgroud)