我正在学习基于“用 C++ 思考”的入门级 C++ 课程。请原谅我可能错过了明显的:
#include <iostream> // cout
using namespace std;
int main(){
const int i = 0;
int* j;
j = const_cast<int*>(&i);
*j = 5;
cout << (&i) << "," << i << "," << j << "," << (*j) << endl;
system("pause");
}
我的系统返回:
'0x22ff74,0,0x22ff74,5'
我只声明了两个变量,一个包含一个 int,一个包含指向同一个 int 的指针。我认为 const_cast 是“从 const 转换为 nonconst”。那么 cout 如何找到三个不同的值呢?