Search code examples
c++multiple-inheritancepure-virtual

c++ Multiple inheritance from 2 functions, while one of them is pure virtual


Let's say we have a class which inherits from 2 other classes which have both a virtual function with the same signature. Because the signature is the same, there will be an error because the main class doesn't know who to inherit the virtual function from. adding to that, if a function is pure virtual, it needs to be initialized in the main class. So my question is, if the main class inherits from 2 classes with same virtual function but one of them is a pure virtual, will it just use the second one?

Or something else needs to be done?

Btw, if possible, I'd like to see the code you wrote to answer this.

Thanks :)


Solution

  • if the main class inherits from 2 classes with same virtual function but one of them is a pure virtual, will it just use the second one?

    No. C++ will not assume that you want that behavior.

    Or something else needs to be done?

    You must implement the function if you don't want the derived class to be abstract.

    void Derived::func()
    {
        SomeBase::func();
    }