21.2. const_cast Operator

C++ provides the const_cast operator for casting away const or volatile qualification. You declare a variable with the volatile qualifier when you expect the variable to be modified by hardware or other programs not known to the compiler. Declaring a variable volatile indicates that the compiler should not optimize the use of that variable because doing so could affect the ability of those other programs to access and modify the volatile variable.

In general, it’s dangerous to use the const_cast operator, because it allows a program to modify a variable that was declared const. There are cases in which it’s desirable, or even necessary, to cast away const-ness. For example, older C and C++ libraries might provide functions ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.