示例example
class B
{
virtual void f(){};
};
class D : public B
{
virtual void f(){};
};
int main(void)
{
B* pb = new D; // unclear but ok
B* pb2 = new B;
D* pd = dynamic_cast(pb); // ok: pb actually points to a D
D* pd2 = dynamic_cast(pb2); // pb2 points to a B not a D, now pd2 is NULL
return 0;
}
父类向子类转化,需要在开始的时候事先实例化子类!