Следующий код:
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" виртуально, устраняет проблему, но я не понимаю, почему. Может кто-нибудь объяснить, что происходит?
p.s. Я пропустил виртуальные деструкторы с целью сделать код короче. Пожалуйста, не говорите мне, чтобы я их ввел, я уже знаю:)