I can’t find much information on const_cast. The only info I could find (on Stack Overflow) is:
The
const_cast<>()is used to add/remove const(ness) (or volatile-ness) of a variable.
This makes me nervous. Could using a const_cast cause unexpected behavior? If so, what?
Alternatively, when is it okay to use const_cast?
const_castis safe only if you’re casting a variable that was originally non-const. For example, if you have a function that takes a parameter of aconst char *, and you pass in a modifiablechar *, it’s safe toconst_castthat parameter back to achar *and modify it. However, if the original variable was in factconst, then usingconst_castwill result in undefined behavior.