I am experimenting with const_cast operator and trying to override a const status of the parameter o passed as an argument:
void function1 (const Object *o)
{
...
function2( const_cast < Object *> ( o ) ); //Exception in g++
}
void function2 (Object *o) {}
But overriding a const status of o throws an exception in g++ (GNU/Linux), in VS 2010 (Win) it works well …
Is there any more reliable way how to override a const status of a function parameter?
Update:
MSDN write: You cannot use the const_cast operator to directly override a constant variable’s constant status :-(.
const_castallows you to stripconstspecifier from the pointer, but it does not affect the “const status” of the value itself. Compiler might decide to put the value into read-only memory (well, it’s const!) and then an attempt to modify it, even throughconst_cast, might result in access violation.Here is a code snippet:
That is,
const_castremoves const specifier during compile time, however it is not its purpose, authority or design to alter access mode of the underlying memory.