C++.Coding.Standards.1918.Rules.Guidelines [Electronic resources]

Herb Sutter, Andrei Alexandrescu

نسخه متنی -صفحه : 521/ 482
نمايش فراداده

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 }