我有以下代码:“可怕的钻石”与多态性
class Base
{
public:
virtual void doSomething() = 0;
};
class BaseImpl : public virtual Base
{
public:
virtual void doSomething() {
// something
}
};
class BaseDerived: public virtual Base
{
public:
virtual void doSomething2() = 0;
};
class BaseDerivedImpl: public BaseImpl, public BaseDerived
{
public:
virtual void doSomething2(){
// sonething2
}
};
然后,我有
Base* b = new BaseImpl();
b->doSomething(); // fatal error at this line (not in the method, but in the method invocation)
的问题是,它甚至没有进入该功能。
使用此类层次结构时出现错误吗?
您的代码编译[here](http://liveworkspace.org/code/yJL1f$29)。你使用什么编译器? – 2013-03-20 13:34:12
是的,它被编译。 GCC(4.7.2)。我有运行时错误 – user14416 2013-03-20 13:36:13
你甚至没有使用BaseDerived或BaseDerivedImpl - 如果你的声明是Base * b = new BaseDerivedImpl;代替?无论如何也是如此。 – 2013-03-20 13:37:20