I understand that const_cast
works with pointers and references.
I\'m assuming that the input to const_cast
should be a pointe
(2) and (3) has the same principle, so I'll talk about (2) only.
The line
const_cast(ri) = 321;
has undefined behavior.
You cannot modify a const
object according to the standard, not even with const_cast
. If you remove const
from a pointer/reference, and modify the pointed/referenced object, the pointed/referenced object must not been declared as const
in the first place.
const_cast
should be used only, when you have a const pointer to something for some reason, and you know that something is not declared as const
.