用RTTI: #include using namespace std; class base { public: virtual void f(){cout << "base";} }; class derived1 : public base { public: virtual void f(){cout << "derived1";} }; class derived2 : public base { public: virtual void f(){cout << "derived2";} }; int main() { base* p1 = new derived1; base* p2 = new derived2; derived1* pd1 = dynamic_cast(p1); if(pd1) cout << "p1 is a pointer to derived1.\n"; derived2* pd2 = dynamic_cast(p2); if(pd2) cout << "p2 is a pointer to derived2.\n"; derived2* pd3 = dynamic_cast(p1); if(pd3) cout << "p1 is a pointer to derived2.\n"; else cout << "p1 is not a pointer to derived2.\n"; }