C ++ complex inheritance with base classes derived from a single class

I came across a problem while trying to reuse code from different classes. I am posting it here in the hope that some of you can help me.

I have a set of classes (B, C) derived from the same class (A), which forces some methods to be implemented (foo, run). Class B implements this method, and both B and C provide other methods:

#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 implementation of foo" << std::endl;
    }

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

    virtual void foobar() const {     // some other function provided by this class
        std::cout << "B 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 implementation of bar with: " << m << std::endl;
    }
};

Now, what I'm trying to do is inherited from both B and C, so I can have additional methods (foobar, bar), but I also don’t need to implement a method from class A (foo), because it is already defined in 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();
    }
};

But for some reason, the compiler gives me this error:

test.cpp: 'int main (int, char **): test.cpp: 68: 35: : 'D < float, double >

A < float, double > * d = D < float, double > ();//

test.cpp: 48: 11: note: 'D < float, double > :

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

    ^

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

virtual void foo() const = 0;//

, :

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

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

    //A<float, double> *c = new C<float, double, int>(); // obviously fails because C does not implement any of A 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;
}

"run" D A. , , , , "B:: run ' . " D:: run" B C, .

, . !

+4
3

B C, A, D, :

template<class I, class O>
class B : virtual public A<I,O> {

// ...

template<class I, class O, class M>
class C : virtual public A<I,O> {

( ()) , , . , .


, , :

class A {
public:
    virtual void foo() = 0;
};

class B : virtual public A {
public:
    virtual void foo() override;
};

void B::foo()
{
    std::cout << "B::foo()" << std::endl;
}

class C : virtual public A { };

class D : public B, public C { };

int main() {
    D d;
    C & c = d;

    c.foo();

    return 0;
}

, C::foo(), , A, B::foo(), A vtable. - , .

+6

@cdhowie .

, , :

struct A
{
   virtual void foo() = 0;
};

struct B : A
{
   virtual void foo() {}
}

struct C : A
{
   void bar() {}
}

struct D : B, C
{
};

D:

A    A
|    |
B    C
 \   /
   D

D , B C. , B A::foo(), C .

, D.

D d;
C* cp = &d;

cp C D , foo . , .

+2

I know this is a late answer, but since you are extracting from a pure virtual function for class C, you must implement it, and then in those functions that you call the base class:

virtual void foo() const {      // for class C  
    B::foo();
}
0
source

All Articles