假设我们有
class Base
{
public:
virtual void foo(){}
};
class Derived: public virtual Base
{};
class Derived_Left: public Derived
{};
class Derived_Right: public Derived
{};
class Bottom: public Derived_Left, public Derived_Right
{};
//让我们讨论案例
//情况1:
void foo()
{
Base *bptr = new Bottom;
Derived *dPtr = dynamic_cast<Derived*>(bptr);//dptr == 0
}
//案例2:
void goo()
{
Bottom *bptr = new Bottom;
Derived *ddtr = dynamic_cast<Derived*>(bptr); // ERROR
}
//main.cpp: 在函数'void goo()'中:
//main.cpp:36:49: 错误:'Derived' 是 'Bottom' 的模糊基数</p>
int main()
{
}
那么,为什么要编译代码以防万一
Base* bptr = new Bottom;
而不是在
Bottom* bptr = new Bottom;