次のコードを検討してください。
struct A
{
virtual void f() = 0;
};
struct B
{
void f();
};
struct C : public A, public B
{};
int main()
{
A* a = new C();
B* b = new C();
C* c = new C();
// All these calls should result in B::f
a->f();
b->f();
c->f();
}
コンパイラはそれC
が抽象的であると述べています。この状況をどのように解決できますか?この問題はダイヤモンドの継承に似ているように見えますが、解決策がわかりません。
編集:ありがとう、これは実際の例です:
#include "stdio.h"
struct A
{
virtual void f() = 0;
};
struct B
{
void f()
{
printf("B::f\n");
}
};
struct C : public A, public B
{
void f()
{
printf("C::f\n");
B::f();
}
};
int main()
{
A* a = new C();
B* b = new C();
C* c = new C();
printf("Calling from A\n");
a->f();
printf("Calling from B\n");
b->f();
printf("Calling from C\n");
c->f();
}
出力:
Calling from A
C::f
B::f
Calling from B
B::f
Calling from C
C::f
B::f