运行时获得指针指向的类信息

const char *cs = typeid(*point).name();

上面的代码可以获得指针指向的类的类型名

下面的代码判断某个指针指向的实例是否是某个类的实例,相当于java的instanceof

#include <iostream>
 
using namespace std;
 
class A{
      virtual void f(){};
};
 
class B: public A{
};
 
int main()
{
    A *a = new B;
    if (typeid(*a) == typeid(B))
    {
         cout << "a is pointed to B object" << endl;
         B *b = dynamic_cast<B*>(a);
    }
    getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/wangjixianyun/p/2827349.html