4

コードをコンパイルしようとすると、コンパイルエラーが発生します。エラーは次のとおりです。

multi.cc: In function ‘int main()’:
multi.cc:35: error: cannot declare variable ‘mdc’ to be of abstract type ‘MostDerivedClass’
multi.cc:27: note:   because the following virtual functions are pure within ‘MostDerivedClass’:
multi.cc:13: note:  virtual int Interface2::common_func()
multi.cc:36: error: request for member ‘common_func’ is ambiguous
multi.cc:13: error: candidates are: virtual int Interface2::common_func()
multi.cc:21: error:                 virtual int InterimClass::common_func()

そしてここに私のコードがあります:

class Interface1 {
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class Interface2 {
public:
    virtual int common_func() = 0;
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};


class InterimClass : public Interface1 {
public:
    virtual int common_func() {
        return 10;
    }
};


class MostDerivedClass : public InterimClass, public Interface2 {
public:
    virtual int new_func() {
        return 20;
    }   
};

int main() {
    MostDerivedClass mdc;
    int x = mdc.common_func();
    cout << "The value = " << x << endl;    

    Interface2 &subset_of_funcs = dynamic_cast<Interface2 &>(mdc);
    x = subset_of_funcs.common_func();
}

私の質問:

  • common_func()がMostDerivedClassの基本クラスであるInterimClassによってすでに実装されていることをコンパイラに伝えるにはどうすればよいですか?

  • 問題を解決する別の方法はありますか?私が本当にやりたいのは、Interface2からcommon_funcも呼び出せるようにすることです。Interface1で大量のメソッドを使用するレガシーコードを使用しています。新しいコードでは、これらのInterface1関数の小さなセットに加えて、追加する必要のあるいくつかの関数を呼び出したいだけです。

4

4 に答える 4

4

からの継承を満たすためにcommon_func()とにかく定義する必要がありますMostDerivedClassInterface2

次のようなものを試すことができます

virtual int common_func() {
    return InterimClass::common_func();
}

これは、最初の 1 つを変更できない場合に最も役立ちます。Interface1

クラス間の真の継承関係が必要な場合は、Lol4t0 のアドバイスに従う必要があります。からスーパークラスを抽出し、Interface1このInterface2新しく作成されたクラスのサブクラスを作成します。例 :

class RootInterface{
public :
    virtual int common_func() = 0;
    virtual ~RootInterface(){}
};

class Interface1 : public virtual RootInterface{
public:
    virtual ~Interface1() {};
};

class Interface2 : public virtual RootInterface{
    public:
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class InterimClass : public Interface1 {
    public:
    virtual int common_func() {
        return 10;
    }
};

class MostDerivedClass : public InterimClass, public Interface2 {
public:
    virtual int new_func() {
        return 20;
    }
};
于 2012-08-12T19:02:49.847 に答える
0

2 番目のインターフェイスを最初のインターフェイスから派生させ、2 番目のインターフェイスvirtual int common_func() = 0;からの宣言を削除し、キーワード virtual を使用してコンパイラを実装に導きます。

class Interface1 {
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class BaseClass : public virtual Interface1 {
public:
    virtual int common_func() {
        return 10;
    }
};

class Interface2 : public virtual Interface1{
public:
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class DerivedClass : public virtual BaseClass, public virtual Interface2 {
public:
    virtual int new_func() {
        return 20;
    }   
};
于 2015-06-26T20:25:48.967 に答える
0

MostDerivedClass にオーバーライドを追加し、そこから InterimClass::common_func() を呼び出します。

于 2012-08-12T19:03:22.573 に答える
0

まず第一に、私はあなたのコードの意味をよく理解していません。

Interface1::common_func のみが実装されていることを知っておく必要があります。

Interface1からInterface2を継承させてみませんか?両方の common_func メソッドを等しくする必要があると思います。

コード例 (ポリモーフィズムを使用):

class Interface1 
{
public:
    virtual int common_func() = 0;
    virtual ~Interface1() {};
};

class Interface2 : public Interface1 {
public:
    virtual int common_func() = 0;
    virtual int new_func() = 0;
    virtual ~Interface2() {};
};

class InterimClass : public Interface2 {
    public:
        virtual int common_func() {
            return 10;
        }
};

class MostDerivedClass : public InterimClass {
public:
    virtual int new_func() {
        return 20;
    }
};

int test_func()
{
    Interface1 * i1 = new MostDerivedClass;
    int x = i1->common_func();
    cout << "The value = " << x << endl;

    Interface2 * i2 = new MostDerivedClass;
    x = i2->common_func();

    return 0;
}
于 2012-08-12T19:14:02.727 に答える