const_cast and reinterpret_cast
Picture an object in a museum display case with a sign that says "look, don't touch". Anyone may look at it, and nobody may touch it. const on a variable is such a sign: the value can be read but not changed.
static_cast changes the type of a value, but it cannot remove const from a pointer:
int main() {
const int limit = 10;
int* p = static_cast<int*>(&limit); // ✗
return *p;
}error: static_cast from 'const int *' to 'int *' is not allowedOther named casts remove const and reinterpret pointers. Each of them has one task.
const_cast
const_cast removes const from a pointer or a reference and changes nothing else: const_cast<int*>(&limit) is an int* to the same limit. It does not change the type of a value, so const_cast<int>(2.5) does not compile. It is used when a function takes an int* but only reads the value:
#include <iostream>
void print_value(int* p) {
std::cout << *p << '\n';
}
int main() {
const int limit = 10;
print_value(const_cast<int*>(&limit)); // 10
}Without const_cast, such a function cannot get a const variable's address: the compiler does not turn const int* into int* itself.
This is where the display-case analogy ends with an important rule. const_cast removes the sign from your pointer only, while the object itself stays a museum piece. If the variable was declared const, it must not be changed even after the cast.
reinterpret_cast and void*
With pointers, reinterpret_cast gives a pointer to another type at the same address. The bytes in memory stay the same: only the type the program reads them through changes. Reading a float through an int* is undefined behavior, so a beginner almost never needs this cast. It does not convert numbers: reinterpret_cast<int>(2.5) does not compile.
Some pointers change type without any cast. An int* converts implicitly to void*, a pointer to a value of unknown type. static_cast<int*> turns it back into an int*.
The fourth named cast, dynamic_cast, works only with pointers and references to compound types such as a struct, never with numbers.
Common mistake
const_cast is used to write to a variable declared const:
int main() {
const int limit = 10;
int* p = const_cast<int*>(&limit);
*p = 20; // ✗
return limit;
}The compiler builds the program with no errors or warnings. But limit is declared const, and writing to it through p is undefined behavior: the standard does not say what main returns. Write through const_cast only to a variable declared without const. If the value has to change, remove const from the declaration.
Key points
const_castremovesconstfrom a pointer or reference and does not change the type of a value.- Writing through
const_castto a variable declaredconstis undefined behavior. reinterpret_castreads the same bytes as another type and does not convert numbers.- An
int*converts implicitly tovoid*, andstatic_cast<int*>turns it back.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.