C++ const_cast


const_cast C++

const_cast is the only function that can change a const object to non-constant type.It’s use is to take away the constantness of an object.Suppose we have an int type pointer to const and we want to assign it to non-constant pointer.We went ahead and assigned the const pointer to non-constant pointer.We ran the program and voila! 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 ;

int* ip=ci; ///error

Now const_cast has a role to play.We will use it to allow the assignment of the pointer to const type to non-const pointer possible.

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

cout<< *ip ;

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


 


Stop wasting time,earn money($$$) from your website-Join Now!

Expressions which the const_cast can cast away the constantness

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

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

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

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

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

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 we are using const_cast to cast a void pointer to string* type. The compiler will complain to this.The case shown is also an intriguing case because the void pointer vd is also a const pointer.To assign the pointer ‘vd’ to string type pointer not only we require a static_cast but we must also perform const_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 that isn’t 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


Conclusion

Besides static_cast and const_cast there are two more casting function :reinterpret_cast and dynamic_cast.We won’t leave any one of them,we will discuss them all ;because we are here to conquer C++.