1 #include <iostream> 2 #include <typeinfo> 3 4 using namespace std; 5 6 class car { 7 public: 8 string name; ~car()9 virtual ~car(){}; 10 }; 11 12 class truck : public car { 13 int colour; 14 }; 15 rtti_test(void)16void rtti_test(void) 17 { 18 double f1 = 1.0; 19 car * pcar; 20 truck *ptruck; 21 22 car tool0; 23 truck tool1; 24 25 pcar = &tool1; 26 27 cout << "rtti test start" << endl; 28 29 cout << "the double type name is :" << typeid(f1).name() << endl; 30 cout << "the tool0 type name is :" << typeid(tool0).name() << endl; 31 cout << "the tool1 type name is :" << typeid(tool1).name() << endl; 32 cout << "the pcar type name is :" << typeid(pcar).name() << endl; 33 34 pcar = &tool1; 35 ptruck = dynamic_cast<truck *>(pcar); 36 if (ptruck == NULL) { 37 cout << "rtti test error" << endl; 38 } 39 40 pcar = &tool0; 41 ptruck = dynamic_cast<truck *>(pcar); 42 if (ptruck != NULL) { 43 cout << "rtti test error" << endl; 44 } 45 46 cout << "rtti test ok" << endl; 47 } 48