C++ const_cast
In this page:
data_type *pointer_name = const_cast<data_type*>(const_pointer);
data_type &reference_name = const_cast<data_type&>(const_reference);
What is const_cast?
The const_cast operator is used to add or remove the const qualifier from a variable. It is primarily used to pass const pointers or references to legacy functions that accept non-const parameters.
Example: What is const_cast?
// Include std::cout and std::cin
#include <iostream>
void legacyPrint(char *msg) {
// Print to the console with cout
std::cout << msg << std::endl;
}
// Program execution starts in main()
int main() {
const char *text = "Hello";
legacyPrint(const_cast<char *>(text));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modifying const References
If an original variable was declared as non-const, you can pass it to a function as a const reference, and then use const_cast inside the function to temporarily modify its value.
Example: Modifying const References
// Include std::cout and std::cin
#include <iostream>
void modify(const int &x) {
int &writable = const_cast<int &>(x);
writable = 100;
}
// Program execution starts in main()
int main() {
// Declare value and set it to 5
int value = 5;
modify(value);
// Print to the console with cout
std::cout << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Undefined Behavior Warning
You must never use const_cast to modify a variable that was originally declared as const. Doing so causes Undefined Behavior, which can corrupt memory or crash your program.
Example: Undefined Behavior Warning
#include <iostream>
int main() {
const int value = 10;
int &ref = const_cast<int &>(value);
// ref = 20; would be undefined behavior: value was truly const
std::cout << "Never modify a truly const original" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
const_cast in Overloaded Methods
A common use for const_cast is to avoid code duplication when overloading member functions for both const and non-const objects. You can implement the non-const method by reusing the const version's logic.
Example: const_cast in Overloaded Methods
// Include std::cout and std::cin
#include <iostream>
// Define the type Data
class Data {
// Declare value and set it to 5
int value = 5;
// Members below can be accessed from outside the class
public:
const int &get() const { return value; }
int &get() {
return const_cast<int &>(static_cast<const Data &>(*this).get());
}
};
// Program execution starts in main()
int main() {
Data d;
d.get() = 42;
// Print to the console with cout
std::cout << d.get() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
const_cast Alternatives
Before using const_cast, consider alternative solutions. If you need to modify a class member variable inside a const function, declare that member variable with the mutable keyword instead.
Example: const_cast Alternatives
// Include std::cout and std::cin
#include <iostream>
// Define the type Cache
class Cache {
mutable int hits = 0;
// Members below can be accessed from outside the class
public:
void access() const { hits++; }
int getHits() const { return hits; }
};
// Program execution starts in main()
int main() {
Cache c;
c.access();
// Print to the console with cout
std::cout << c.getHits() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
const_castto modify an object that was originally declaredconst, which is undefined behavior. - Using
const_castto silence const errors instead of fixing the design. - Expecting
const_castto change types other than adding or removing const or volatile.
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced