// ---------------------------------------------------------------------- // // casts.cc - Test behavior of const_cast<> and static_cast<>. // // History: // 29-Jun-1999 WEB Initial draft, adapted from Blitz++ // 25-Apr-2001 WEB s//"ISOcxx\/ISOcxxSyntax.hh"/ // // ---------------------------------------------------------------------- #include "ISOcxx/ISOcxxSyntax.hh" struct Dog { Dog() { ; } virtual int fetch() = 0; virtual int fetch() const = 0; }; // Dog struct Puppy : public Dog { Puppy() { ; } virtual int fetch() ; virtual int fetch() const; }; // Puppy int Puppy::fetch() { return 1; } int Puppy::fetch() const { return 2; } void foo( Puppy const & ) { ; } int main() { // check static_cast: Puppy pup1; Dog & dog1 = pup1; Puppy & pup2 = STATIC_CAST( Puppy &, dog1 ); foo( pup2 ); if ( pup2.fetch() != 1 ) // ought be non-const version return 1; // check const_cast: Puppy const pup3; if ( pup3.fetch() != 2 ) // ought be const version return 2; if ( CONST_CAST( Puppy &, pup3 ).fetch() != 1 ) // ought be non-const version return 3; return 0; } // main()