C ++ Mehrfachvererbung mit Basisklassen, die von derselben Klasse abgeleitet sind

Ich bin auf ein Problem gestoßen, als ich versuchte, Code aus verschiedenen Klassen wiederzuverwenden. Ich poste es hier in der Hoffnung, dass einige von Ihnen mir helfen können.

Ich habe eine Reihe von Klassen (B, C), die von derselben Klasse (A) abgeleitet sind, wodurch die Implementierung einiger Methoden (foo, run) erzwungen wird. Klasse B implementiert diese Methode, und sowohl B als auch C bieten andere Methoden:

#include<iostream>

template<class I, class O>
class A {
public:
    A() {}
    virtual ~A() {}

    virtual void foo() const = 0;     // force implementation of this function
    virtual void run() const = 0;     // force implementation of this function
};

template<class I, class O>
class B : public A<I,O> {
public:
    B() {}
    virtual ~B() {}

    virtual void foo() const {        // implementation for the Base class
        std::cout << "B's implementation of foo" << std::endl;
    }

    virtual void run() const {        // implementation for the Base class
        std::cout << "B's implementation of run" << std::endl;
    }

    virtual void foobar() const {     // some other function provided by this class
        std::cout << "B's implementation of foobar" << std::endl;
    }
};

template<class I, class O, class M>
class C : public A<I,O> {
public:
    C() {}
    virtual ~C() {}

    virtual void bar(M m) const {     // some other function provided by this class
        std::cout << "C's implementation of bar with: " << m << std::endl;
    }
};

Now, was ich versuche zu tun, ist von B und C zu erben, so dass ich die zusätzlichen Methoden (foobar, bar) haben kann, aber auch nicht die Methode aus Klasse A (foo) implementieren muss, weil es bereits in definiert ist B:

template<class I, class O>
class D : public B<I,O>, public C<I,O,int> {
public:
    D() {}

    void run() const {
        this->bar(123);
        this->foo();
        this->foobar();
    }
};

Aber aus irgendeinem Grund gibt der Compiler mir diesen Fehler:

test.cpp: In der Funktion 'int main (int, char **)': test.cpp: 68: 35: Fehler: Objekt vom abstrakten Typ 'D <float, double>' kann nicht zugeordnet werden

A <float, double> * d = new D <float, double> (); // was ich tun mus

test.cpp: 48: 11: note: da die folgenden virtuellen Funktionen in "D <float, double>" rein sind:

class D: public B <I, O>, public C <I, O, int>

    ^

test.cpp: 9: 22: note: void A <I, O> :: foo () const [mit I = float; O = double]

virtual void foo () const = 0; // Implementierung dieser Funktion erzwingen

Dies ist der Code, den ich verwende, um es auszuführen:

int main(int argc, char **argv)
{

    A<float, double> *b = new B<float, double>();
    b->foo();                                            // prints "B's implementation of foo"
    b->run();                                            // prints "B's implementation of run"

    //A<float, double> *c = new C<float, double, int>(); // obviously fails because C does not implement any of A's functions

    //A<float, double> *d = new D<float, double>;        // line 68: what I need to do
    //d->run();                                          // ***throws the abstract class error

    return 0;
}

Ich möchte die 'run'-Funktion eines Objekts der Klasse D von einem Zeiger auf ein A verwenden. Da alle Funktionen virtuell sind, erwarte ich, die Implementierung jeder im niedrigsten Vererbungspunkt definierten Funktion auszuführen, was bedeutet, dass' B :: run 'wird verworfen. Da 'D :: run' Funktionen von B und C verwendet, muss ich von beiden Klassen erben.

Ich hoffe ich habe es genug beschrieben und niemanden verwirrt. Danke für die Hilfe

Antworten auf die Frage(3)

Ihre Antwort auf die Frage