【发布时间】:2013-02-27 12:21:38
【问题描述】:
假设我们有
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
{};
//让我们讨论案例
//案例一:
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’的模糊基数
int main()
{
}
那么,为什么要在以下情况下编译代码
Base* bptr = new Bottom;
而不是
Bottom* bptr = new Bottom;
【问题讨论】:
-
首先我想提一下我第一次在ubuntu g++下编译它。它给了我这个错误。但是在家里我试图在 windows/visual stuio 2012 下编译它,它只给了我一个警告警告 C4540: dynamic_cast used to convert to inaccessible or ambiguous base;运行时测试将失败('Bottom *' to 'Derived *')
标签: inheritance multiple-inheritance dynamic-cast