Exceptions
Team LiB
Previous Section Next Section

Exceptions

Casting away const can be necessary to call const-incorrect APIs (see Item 15). It is also useful when a function that must take and return the same kind of reference has const and non-const overloads, implemented by having one call the other:



const Object& f( const Object& );





Object& f( Object& obj ) {


  const Object& ref = obj;


  return const_cast<Object&>( foo(ref) );   // have to const_cast the return type


}



    Team LiB
    Previous Section Next Section