C++ const_cast Keyword
Example
Remove const from a pointer, so you can change the value it points to:
int myNum = 15;
const int* ptr = &myNum;
int* ptr2 = const_cast<int*>(ptr);
*ptr2 = 20;
cout << myNum;
Definition and Usage
The const_cast keyword converts a value by adding or removing const. It is mostly used to remove const from a pointer or reference, so you can change the value it points to.
Only change a value this way if the variable itself was not created as const, like myNum in the example. Changing a variable that was created as const gives undefined behavior (the program can do anything).
Related Pages
Read more about pointers in our C++ Pointers Tutorial, and about other conversions in our C++ Type Conversion Tutorial.