它使您能够在运行时识别对象的动态类型。例如:
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)
| 归档时间: |
|
| 查看次数: |
3804 次 |
| 最近记录: |