C++中的运行时类型信息

gop*_*pal 5 c++ rtti

什么是C++中的运行时类型控件?

Nav*_*een 4

它使您能够在运行时识别对象的动态类型。例如:

class A
{
   virtual ~A();
};

class B : public A
{
}

void f(A* p)
{
  //b will be non-NULL only if dynamic_cast succeeds
  B* b = dynamic_cast<B*>(p);
  if(b) //Type of the object is B
  {
  }
  else //type is A
  { 
  }
}

int main()
{
  A a;
  B b;

  f(&a);
  f(&b);
}
Run Code Online (Sandbox Code Playgroud)