For example:
const int* pc = new const int(3); // note the const
int* p = const_cast<int*>(pc);
*p = 4; // undefined behavior?
In particular, can the compiler ever optimize away the heap-allocated *pc
?
If not, does an attempt to modify *pc
via p
still constitute undefined behavior - and if so, why?
Yes and yes. As to why - because you're modifying a
const
object.And good point about the
const
afternew
- without it, the code would be legal.