Possible Duplicate:
Correct usage(s) of const_cast<>
What is the use of having a const
parameter in the function argument and then using const_cast
within the function?
Possible Duplicate:
Correct usage(s) of const_cast<>
What is the use of having a const
parameter in the function argument and then using const_cast
within the function?
I've only used const_cast on cases where I have to pass a const parameter to unmodifiable legacy code known to not modify the parameter, i.e. it is in practice const but the signature of the API/function call does not contain the const keyword.
You'd only use it when you need to pass that parameter through to a legacy C API that promises not to modify the data.
In no other case should you need to hack away constness with const_cast
, and be aware that even after hacking away constness it's still undefined behavior to modify the object; it's just not enforced by your compiler any more, and you take on responsibility for that promise.