I have a small test file.....
const int iValue = 10 ;
int *ptr = const_cast<int *> &iValue ;
*ptr = 20 ;
cout << "Address :: " << &iValue << ", " << ptr << endl ;
cout << "Value :: " << iValue << ", " << *ptr << endl ;
return 0 ;
I have defined a const int with value 10.... and removed its const property using const_cast and modified its value..... But when you print their values.... they differ... even though their addresses are same...... Even though morally you shouldn't do such things..... But I am wondering how this can be possible......
So I checked the Assembly output of the program And I got the answer..... compiler replaced all the instances of iValue with 10 directly without refering to the actual memory location of iValue.......
So I concluded this is some sort of optimization.... First of all I want to ask whether I am correct or not... if yes how can I stop the compiler to stop the optimization....
thanks in advance