Const cast

int main() {
const int i = 0;
int* j = (int*)&i; // Deprecated form
j = const_cast<int*>(&i); // Preferred
// Can't do simultaneous additional casting:
*j=20;
cout<<*j<<"addresss "<<(int)j<<endl;
cout<<i<<"addresss "<<int(&i)<<endl;

} ///:~


The follwing code produces this output...

20addresss 2293620
0addresss 2293620

I fail to understand why?
Because you can't modify a const variable. ( actually it's a constant, not a variable )
http://www.cplusplus.com/forum/general/17155/
http://www.cplusplus.com/forum/general/17731/
Last edited on
Thanks :)
Topic archived. No new replies allowed.