typeid参数

来源:互联网 发布:汇航亿胜网络推手公司 编辑:程序博客网 时间:2024/06/05 20:13

http://msdn.microsoft.com/en-us/library/fyf39xec(v=VS.80).aspx

If the expression points to a base class type, yet the object is actually of a type derived from that base class, a type_info reference for the derived class is the result. The expression must point to a polymorphic type (a class with virtual functions). Otherwise, the result is the type_info for the static class referred to in the expression. Further, the pointer must be dereferenced so that the object it points to is used. Without dereferencing the pointer, the result will be the type_info for the pointer, not what it points to. For example:

// expre_typeid_Operator.cpp// compile with: /GR /EHsc#include <iostream>#include <typeinfo.h>class Base {public:   virtual void vvfunc() {}};class Derived : public Base {};using namespace std;int main() {   Derived* pd = new Derived;   Base* pb = pd;   cout << typeid( pb ).name() << endl;   //prints "class Base *"   cout << typeid( *pb ).name() << endl;   //prints "class Derived"   cout << typeid( pd ).name() << endl;   //prints "class Derived *"   cout << typeid( *pd ).name() << endl;   //prints "class Derived"   delete pd;}

原创粉丝点击