C ++中的运行时类型信息 [英] Run-time type information in C++
本文介绍了C ++中的运行时类型信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是C ++中的运行时类型控制?
What is runtime type control in C++?
推荐答案
它使您能够在运行时识别对象的动态类型.例如:
It enables you to identify the dynamic type of a object at run time. For example:
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);
}
这篇关于C ++中的运行时类型信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文