Множественное наследование и чисто виртуальные функции

Следующий код:

struct interface_base
{
    virtual void foo() = 0;
};

struct interface : public interface_base
{
    virtual void bar() = 0;
};

struct implementation_base : public interface_base
{
    void foo();
};

struct implementation : public implementation_base, public interface
{   
    void bar();
};

int main()
{
    implementation x;
}

не может быть скомпилирован из-за следующих ошибок:

test.cpp: In function 'int main()':
test.cpp:23:20: error: cannot declare variable 'x' to be of abstract type 'implementation'
test.cpp:16:8: note:   because the following virtual functions are pure within 'implementation':
test.cpp:3:18: note:    virtual void interface_base::foo()

Я поигрался с ним и выяснил, что создание 'interface -> interface_base' и 'implementation_base - > interface_base 'наследование virtual, устраняет проблему, но я не понимаю почему. Может кто-нибудь объяснить, что происходит?

p.s. Я специально опустил виртуальные деструкторы, чтобы код был короче. Пожалуйста, не просите меня вставлять их, я уже знаю :)

16
задан HC4 - reinstate Monica 31 July 2012 в 05:13
поделиться