here is my code:
#include <iostream>
using namespace std;
int main()
{
const int a = 10;
cout << a << endl;
int *b = const_cast<int *>(&a);
cout << *b << endl;
(*b)++;
cout << *b << endl;
cout << a << endl;
return 0;
}
output on ubuntu20, g++ 9.3.0
-----
10
10
11
10
the output puzzles me. pointer b points to int a. I have cast it to int *. why the output for a is not changed and the b has changed. what's the mechanism of const_cast working. I am a new learner of cpp, I used java.