When I was trying to figure out top-level const and const_cast, I wrote some code as follows.
int main()
{
// m is a top-level const
const int m = 10;
// this is an undefined behavior according to *Primer c++*
// but I still compile and run this without warning.
int *q = const_cast<int *>(&m);
(*q)++;
//Then I print the address and the value
cout<< "the value of address "<< q <<" is "<< *q <<endl;
cout<< "the value of address "<< &m <<" is "<< m <<endl;
return 0;
}
The print result makes me confused.
the value of address 0x7ffee6a43ad8 is 11
the value of address 0x7ffee6a43ad8 is 10
Is that one of the undefined Behaviours? What really happened when I do "(*q)++"?
Thanks in advance