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.

Syntax

const_cast<new_type>(expression)

**Where:

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

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

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

**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

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

Limitations of const_cast

While const_cast is useful in specific situations, it has several limitations: