jeudi 26 août 2021

How to inherit an implementation of an interface from another base class not related to the interface [duplicate]

Is it possible to inherit a pure virtual function implementation from another class like the following, without having to define a wrapper around Base::foo() in Derived ?

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

class Base
{
public:
    void foo() {};
};

class Derived
    : public Interface
    , public Base
{
};

int main()
{
    Derived derived;
    derived.foo();
}

The above gives the following errors:

 In function 'int main()':
21:13: error: cannot declare variable 'derived' to be of abstract type 'Derived'
13:7: note:   because the following virtual functions are pure within 'Derived':
4:18: note:     virtual void Interface::foo()
22:13: error: request for member 'foo' is ambiguous
10:10: note: candidates are: void Base::foo()
4:18: note:                 virtual void Interface::foo()

I would like to avoid defining a wrapper like the following, especially when multiple methods are concerned:

class Derived
{
public:
    void foo() override
    {
        Base::foo();
    }
};

I tried adding using Base::foo; in Derived body, but it only removes the error about ambiguous call, not the one stating that Derived is an abstract class.

Aucun commentaire:

Enregistrer un commentaire