const_cast in C++ | Type Casting operators (original) (raw)
Last Updated : 11 Jun, 2026
The const_cast operator is used to add or remove const and volatile qualifiers from pointers, references, and object types. It is commonly used when interacting with APIs that do not support const-correctness or when accessing non-const objects through const-qualified references or pointers.
- Cannot change the underlying data type of an object.
- Often used with pointers and references.
- Modifying an object that was originally declared as const results in undefined behavior.
Syntax
const_cast<new_type>(expression)
**Where:
- new_type is the target type.
- expression is the object, pointer, or reference being cast.
Applications of const_cast
The const_cast operator is commonly used in the following situations:
Modifying Non-Const Members Inside a const Member Function
In a const member function, the this pointer is treated as a pointer to a const object. const_cast can be used to remove this const qualification when modifying non-const data members.
CPP `
#include using namespace std;
class student { private: int roll; public: // constructor student(int r):roll(r) {}
// A const function that changes roll with the help of const_cast
void fun() const
{
( const_cast <student*> (this) )->roll = 5;
}
int getRoll() { return roll; }};
int main(void) { student s(3); cout << "Old roll number: " << s.getRoll() << endl;
s.fun();
cout << "New roll number: " << s.getRoll() << endl;
return 0;}
`
**Output
Old roll number: 3
New roll number: 5
**Explanation
- fun() is a const member function.
- Inside a const function, object members cannot normally be modified.
- const_cast removes the const qualification from this.
- Since roll is not declared as const, modifying it is valid.
Passing const Data to a Function That Expects a Non-Const Pointer
Some older APIs accept non-const pointers even though they do not modify the data. In such cases, const_cast can be used to remove the const qualifier.
CPP `
#include using namespace std;
int fun(int* ptr) { return (*ptr + 10); }
int main(void) { const int val = 10; const int *ptr = &val; int *ptr1 = const_cast <int *>(ptr); cout << fun(ptr1); return 0; }
`
**Output
20
**Explanation
- fun() expects an int*.
- ptr is a const int*.
- const_cast removes the const qualifier, allowing the pointer to be passed to the function.
Modifying an Object Originally Declared const
Removing constness does not make a truly const object modifiable.
CPP `
#include using namespace std;
int fun(int* ptr) { *ptr = *ptr + 10; return (*ptr); }
int main(void) { const int val = 10; const int *ptr = &val; int *ptr1 = const_cast <int *>(ptr); fun(ptr1); cout << val; return 0; }
`
**Output
Undefined Behavior
**Explanation
- val is originally declared as const.
- Modifying it through a casted pointer results in undefined behavior.
- The program may produce different results depending on the compiler and system.
**Note: If the original object is not declared as const, removing constness and modifying it is valid.
CPP `
#include using namespace std;
int fun(int* ptr) { *ptr = *ptr + 10; return (*ptr); }
int main(void) { int val = 10; const int *ptr = &val; int *ptr1 = const_cast <int *>(ptr); fun(ptr1); cout << val; return 0; }
`
Type Safety of const_cast
Unlike C-style casts, const_cast can only modify const and volatile qualifiers. It cannot convert between unrelated types.
C `
#include using namespace std;
int main(void) { int a1 = 40; const int* b1 = &a1; char* c1 = const_cast <char *> (b1); // compiler error *c1 = 'A'; return 0; }
`
**output
prog.cpp: In function ‘int main()’:
prog.cpp:8: error: invalid const_cast from type 'const int*' to type 'char*'
**Explanation
- const_cast only adds or removes const and volatile qualifiers.
- It cannot convert an int* to a char*.
- Such conversions result in a compilation error.
Removing volatile Qualifiers
The const_cast operator can also remove the volatile qualifier from pointers and references.
CPP `
#include #include using namespace std;
int main(void) { int a1 = 40; const volatile int* b1 = &a1; cout << "typeid of b1 " << typeid(b1).name() << '\n'; int* c1 = const_cast <int *> (b1); cout << "typeid of c1 " << typeid(c1).name() << '\n'; return 0; }
`
**Output
typeid of b1 PVKi
typeid of c1 Pi
**Explanation
- b1 is a pointer to a const volatile integer.
- const_cast removes the const and volatile qualifiers.
- The resulting pointer type becomes int*.
Limitations of const_cast
While const_cast is useful in specific situations, it has several limitations:
- Can only add or remove const and volatile qualifiers.
- Cannot convert between unrelated data types.
- Does not make a truly const object safely modifiable.
- Incorrect usage can result in undefined behavior.