I have a big confusion about how a c++ compiler treating with a const variable
const int constfunc()
{
return 7;
}
const int u1 = constfunc();
int* pu1 = const_cast<int*>(&u1);
*pu1 = 10;
cout << u1 << endl;
cout << *pu1 <<endl;
The result of precending code is: 10 10
While I try to assign a literal value to u1 variable neither than a const function, I mean:
const int u1 = 7;
The result will be changed: 7 10
I have really two confusions: 1- I made a little change but the result different? 2- As I know a const_cast remove a const restriction on a const pointers which point to none const variables, but in my sample1 I can modify value of const variable "u1"?