// 動的に型を認識しているが、 // ポインタ 0 をtypeidが参照した時は bad_typeid 例外をスローする。 // (*) コンパイラオプションに /GR が必要です。 // usage : cl /GX /GR rtti02.cpp #include <iostream> #include <typeinfo> using namespace std; class basic { int i; public: virtual void foo( ) {} // ... }; class sub1 : public basic { int ihoge; // ... }; class sub2 : public sub1 { int ihogehoge; // ... }; void print_type( basic *pBasic ) { cout << "type is : " << typeid( *pBasic ).name( ) << endl; } int main( ) { basic o_basic,*po; sub1 o_sub1; sub2 o_sub2; try { po = &o_basic; print_type( po ); po = &o_sub1; print_type( po ); // 例外スロー po = 0; print_type( po ); } catch ( bad_typeid ) { cout << "catch a bad_typedid exception." << endl; } catch ( ... ) { cout << "catch a ... exception." << endl; } return 0; } /* bad_typeid が捕捉できてませんが(? bug ?)、 とりあえず例外はスローされていることだけは確認できました。 */ // end of file