我有以下代碼:「可怕的鑽石」與多態性
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