Given this code (from my last post here):
const int j = 5; // constant object
const int *p = &j; // `p` is a const access path to `j`
int *q = const_cast<int *>(p); // `q` is a non-const access path to `j`
*q = 10;
cout << *q << endl;
The output is : 10
Is it suppose to be this way ? I thought that this code was supposed to lead to
an undefined behavior , since j is a const . Am I wrong ?
Thanks
Undefined behavior can be anything — it could do exactly what you want to it do, or it could destroy the universe. If possible, avoid undefined behavior since I don’t really want to be destroyed just because you’re too lazy to do things correctly.