C++ Programming Guide

const_cast

The expression const_cast<T>(v) can be used to change the const or volatile qualifiers of pointers or references. (Among new-style casts, only const_cast<> can remove const qualifiers.) T must be a pointer, reference, or pointer-to-member type.


class A 
{ 
public: 
  virtual void f();
  int i; 
};
extern const int A::* cimp;
extern const volatile int* cvip;
extern int* ip;
void use_of_const_cast( )
{ 
  const A a1;
  const_cast<A&>(a1).f( );                // remove const
  a1.*(const_cast<int A::*> cimp) = 1;    // remove const
  ip = const_cast<int*> cvip;    // remove const and volatile
}