純粋な仮想クラスである基本クラス 'Base' があります。
class Base {
public:
virtual void A() = 0;
virtual void B() = 0;
virtual ~Base() { } // Eclipse complains that a class with virtual members must have virtual destructor
};
他にも 2 つのクラスがあり、そのうちの 1 つは A() を実装し、もう 1 つは B() を実装しています。
class DerivedA : public virtual Base
{
public:
virtual void A() {
printf("Hello from A");
}
};
class DerivedB : public virtual Base
{
public:
virtual void B() {
printf("Hello from B");
}
};
宣言の virtual キーワードは、ひし形の問題を解決するはずです。
次のように、A() と B() の両方が実装されるように、2 つのクラスを別のクラスに結合したいと思います。
class DerivedC: public DerivedA, public DerivedB {
// Now DerivedA and DerivedB are combined
};
// Somewhere else in the code
DerivedC c;
c.A();
c.B();
問題:
G++ はコードを正常にコンパイルしますが、Eclipse はエラーを出します: The type 'DerivedC' must implement the inherited pure virtual method 'Base::B'
. Visual Studio でコンパイルすると、次の 2 つの警告が表示されます。
warning C4250: 'DerivedC' : inherits 'DerivedB::DerivedB::B' via dominance
warning C4250: 'DerivedC' : inherits 'DerivedA::DerivedA::A' via dominance
問題は次のとおりです。これを行う正しい方法は何ですか? 上記のコードは未定義の動作を生成しますか?
注:タイトルは少し誤解を招く可能性があります。この質問の適切なタイトルがわかりません。