Casting Away Const

Quite simply, use of a typecast (or some other method) to tell a compiler to disregard the ConstQualifier, and allow an object declared const to be mutated.

CeePlusPlus even has a special purpose operator for this; const_cast<T>. (You can also use the general purpose CeeLanguage cast operator for this; however this is considered deprecated in C++). Other new-style C++ casts (such as dynamic_cast<T> (DynamicCast) and static_cast<T>) don't let you discard const. (They don't let you discard the VolatileQualifier either; for that you use const_cast as well...)

CastingAwayConst has two legitimate uses: Dealing with a legacy interface which is ConstIncorrect, or implementing LogicalConstness if other means of doing so (such as mutable) are not provided.


EditText of this page (last edited August 13, 2009) or FindPage with title or text search