例如:
class Animal
{
virtual void dummy() {}; //LINE1
}
class Cat : public Animal
{
}
Animal* a = new Cat();
if (Cat* c = dynamic_cast<Cat*> (a)) //LINE2
{
//Do something.
}
如果我从Animal类中删除LINE1(即Animal类不包含虚成员),LINE2将无法工作。
例如:
class Animal
{
virtual void dummy() {}; //LINE1
}
class Cat : public Animal
{
}
Animal* a = new Cat();
if (Cat* c = dynamic_cast<Cat*> (a)) //LINE2
{
//Do something.
}