A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.wikibooks.org/wiki/C++_Programming/Programming_Languages/C++/Code/Keywords/const_cast below:

C++ Programming - Wikibooks, open books for an open world

From Wikibooks, open books for an open world

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.

Syntax
    TYPE* const_cast<TYPE*> (object);
    TYPE& const_cast<TYPE&> (object);

For example, the following code uses const_cast to remove the const qualifier from an 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
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4