Modifying a const through a non-const pointer
As I said in my comment, once you modified the const value you are in undefined behaviour land, so it doesn't make much sense to talk about what is happening. But what the hell..
cout << *w << endl; // (3) outputs 5
cout << e << endl; // (4) outputs 2
At a guess, *w
is being evaluated at runtime, but e
is being treated as a compile time constant
I suspect that you're tripping up the compiler. It doesn't expect you to play dirty tricks with e, so when it sees the line:
cout << e << endl;
It simply inserts the value 2 instead of looking for the actual value. You can verify (or disprove) this by looking at the disassembly of your program.