次のコード:
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」の継承を仮想化すると問題が解決することを理解しましたが、理由はわかりません。誰かが何が起こっているのか説明してもらえますか?
psコードを短くするために、意図的に仮想デストラクタを省略しました。それらを入れるように私に言わないでください、私はすでに知っています:)