C++ const_cast casting

In C++ const_cast is the only function that can change a const pointer to non-constant type.It’s purpose is to take away the constantness of a pointer.

Suppose we have a const int type pointer and we want to assign it to non-constant type pointer.We went ahead and assigned the const pointer to non-constant pointer.But when we ran the program we got an error message “invalid conversion from const int* to int*”.The message is clear we cannot assign const pointer to non-const pointer.A code example is given below.

int i=890;
const int* ci=&i ; //Work fine

int* ip=ci; //error!!

Now const_cast comes into to play.We will use it to allow the assignment of the const type pointer to non-const pointer.

int *ip=const_cast<int*>(ci) ; //work fine

cout<< *ip ; 


In using const_cast the pointer ‘ci’ is treated as a normal pointer and so assigning it to non-const pointer ‘ip’ is possible.

Some expressions whose constantness can be cast away with const_cast

Pointer and reference

const_cast can cast away the constantness of only pointers and reference, not the constnesss of object or variable or any other expression.

const int val=89 ;
int i1=54;
const int &refval=val ;
int *const cpt=&i1 ;

cout<< ++const_cast< int >(val) ; //error!!

cout<< ++const_cast< int& >(refval) ; //work fine ,gives 90

cout<< ++*(const_cast< int* >(cpt) ) ; //work fine, gives 55

Sometimes we also use const pointer and pointer to const type.For such object also const_cast can cast away their constantness.

const int *const cic=&val ;

int *inew=const_cast< int* > (cic) ; //work fine

const_cast can perform only constant casting

The function of static_cast is to cast one type to another type and it cannot perform any other type of casting.Likewise const_cast can only cast away the constantness of an object it cannot perform any other type of casting.

Link :Static_cast

const string *st=”Merry Christmas”;

string *s=static_cast< string* > (st) ; //error!!

const void *vd=st ;

string *ss=const_cast< string* >(vd) ; //error!!

In the code above we used const_cast to cast a void pointer to string* type obviously the compiler will complain to this.

Since ‘vd’ is const type we require const_cast to cast away the constantness but to convert ‘vd’ to string type we also require to perform a static_cast.So how do we do it? The logic is simple ,first of all we need to take away the constantness of the pointer and then we can perform the type casting.The code is shown below.

string *ss=static_cast<string*>(const_cast< void* >(vd) ) ;

cout<< *ss ;

I am sure the implementation process is not hard.Knowing which to cast first is the entire base solution of the problem.

volatile type casting

const_cast can also cast away the volatileness of the type.This means it can cast a volatile type to normal type.Consider the code below.

volatile int val=890 , volPt=&val ;

int *ipt=volPt ; //error!!

int *ii=const_cast<int*>(volPt) ;//work fine


Besides static_cast and const_cast there are two more casting function :reinterpret_cast and dynamic_cast.We won’t leave out any one of these,we will discuss them all.

Leave a Reply

Your email address will not be published. Required fields are marked *