#include<iostream>
using namespace std;
int main()
{
const int i=10;
int *p=const_cast<int *>(&i);
*p=200;
cout<<p<<" "<<*p<<endl;
cout<<&i<<" "<<i<<endl;
return 0;
}
This code is a prime example of why casting is bad news in C++.
In this case, i is a constant with a value of 10. That can never change.
When you cast, you are asking the compiler to take the address of the constant and make it a non-constant. The compiler won't do that because it knows you will use the cast to change the constant. So, it
makes a copy of i and hides it. Now whenever you use *p you see the int at the original address but when you use i, the compiler trots out its copy and lets you use that.
You have noticed that the address of i and the address in the pointer are the same. This is courtesy of the compiler. It tries to make you think both values are at the same location.
This is so ugly because the part of your program using i uses a different value than the part that uses *p.
The cast has destroyed the type safety of your program.
If you try this in C you get a different result. In C, you get to change the constant because C is really crude in this area.