const_cast [C++ Reference]
Keywords: keywords,const_cast
Date: 2009-03-25T01:39:58-0700

[[const_cast]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- cz
- de
- es
- fr
- it
- jp
- nl
- pl
- ro
- ru
- sk
- tr
- tw

## const_cast

Syntax:

        TYPE const_cast<TYPE> (object);

The const_cast keyword can be used to remove the const or volatile property from an object. The target data type must be the same as the source type, except (of course) that the target type doesn't have to have the same const qualifier. The type TYPE must be a pointer or reference type.

For example, the following code uses const_cast to remove the const qualifier from a object:

    class Foo {
    public:
      void func() {} // a non-const member function
    };

    void someFunction( const Foo& f )  {
      f.func();      // compile error: cannot call a non-const
                     // function on a const reference
      Foo &fRef = const_cast<Foo&>(f);
      fRef.func();   // okay
    }

Related Topics: const, dynamic_cast, reinterpret_cast, static_cast, A comparison of the C++ casting operators

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •