// typeid( ) の値を演算子== で比較できる。 // 動的に型が等しいか否かを確認できることを意味する。 // (*) コンパイラオプションに /GR が必要です。 // usage : cl /GX /GR rtti03.cpp #include <iostream> #include <typeinfo> using namespace std; class basic { int i; public: virtual void foo( ) {}; // ... }; class sub1 : public basic { int ihoge; // ... }; int main( ) { int i,j; basic o_basic; sub1 o_sub1; if ( typeid( i ) == typeid( j ) ) { cout << "i and j are same type." << endl; } else { cout << "i and j are different type." << endl; } if ( typeid( o_basic ) == typeid( o_sub1 ) ) { cout << "o_basic and o_sub1 are same type." << endl; } else { cout << "o_basic and o_sub1 are different type." << endl; } return 0; } /* 同様に演算子 != も使用できます。 */ // end of file