#include <iostream> #include <typeinfo> using namespace std; class Base { public: virtual void func(){cout << "I, Base." << endl;} }; class Derived:public Base { public: void dfunc(){cout << "I, Derived." << endl;} }; class BadDerived { public: virtual void func(){cout << "I, BadDerived." << endl;} }; class T { public: void dfunc(){cout << "I, T." << endl;} }; int main() { Base *baseptr = new Derived; BadDerived *badptr = new BadDerived; cout << endl << "*********Test 0************" << endl; if(T *deriveptr = dynamic_cast<T*>(baseptr)) { cout << "success---->"; deriveptr->dfunc(); } else { cout << "failed----->"; baseptr->func(); cout << "DEBUG" << endl; cout << "typeid(T).name = " << typeid(T).name() << endl; cout << "typeid(Baseref).name = " << typeid(*baseptr).name() << endl; } cout << endl << "*********Test 1************" << endl; if(Derived *deriveptr = dynamic_cast<Derived*>(baseptr)) { cout << "success---->"; deriveptr->dfunc(); } else { cout << "failed----->"; baseptr->func(); cout << "DEBUG" << endl; cout << "typeid(Derived).name = " << typeid(Derived).name() << endl; cout << "typeid(Baseref).name = " << typeid(*baseptr).name() << endl; } cout << endl << "*********Test 2************" << endl; if(Derived *deriveptr = dynamic_cast<Derived*>(badptr)) { cout << "success---->"; deriveptr->dfunc(); } else { cout << "failed----->"; badptr->func(); cout << "DEBUG" << endl; cout << "typeid(Derived).name = " << typeid(Derived).name() << endl; cout << "typeid(Baseref).name = " << typeid(*badptr).name() << endl; } Derived deriveobj; Base &baseref = deriveobj; BadDerived badobj; BadDerived &badref = badobj; cout << endl << "*********Test 3************" << endl; try { Derived &deriveref = dynamic_cast<Derived&>(baseref); cout << "success---->"; deriveref.dfunc(); } catch(bad_cast) { cout << "failed----->"; baseref.func(); cout << "DEBUG" << endl; cout << "typeid(Derived).name = " << typeid(Derived).name() << endl; cout << "typeid(Baseref).name = " << typeid(baseref).name() << endl; } cout << endl << "*********Test 4************" << endl; try { Derived &deriveref = dynamic_cast<Derived&>(badref); cout << "success---->"; deriveref.dfunc(); } catch(bad_cast) { cout << "failed----->"; baseref.func(); cout << "DEBUG" << endl; cout << "typeid(Derived).name = " << typeid(Derived).name() << endl; cout << "typeid(Baseref).name = " << typeid(badref).name() << endl; } cout << endl << "*********Test 5************" << endl; if(typeid(Derived) == typeid(baseref)) { Derived &deriveref = dynamic_cast<Derived&>(baseref); cout << "success---->"; deriveref.dfunc(); } else { cout << "failed----->"; baseref.func(); cout << "DEBUG" << endl; cout << "typeid(Derived).name = " << typeid(Derived).name() << endl; cout << "typeid(Baseref).name = " << typeid(baseref).name() << endl; } cout << endl << "*********Test 6************" << endl; if(typeid(Derived) == typeid(badref)) { Derived &deriveref = dynamic_cast<Derived&>(badref); cout << "success---->"; deriveref.dfunc(); } else { cout << "failed----->"; badref.func(); cout << "DEBUG" << endl; cout << "typeid(Derived).name = " << typeid(Derived).name() << endl; cout << "typeid(Baseref).name = " << typeid(badref).name() << endl; } }
原文地址:http://blog.csdn.net/pngynghay/article/details/42679791