0

Why does this simple class hierarchy end up in a abstract class B? I don't know a buzz word to search for in this case. (Maybe Diamond-Problem?)

class I_A
{
    virtual void foo() = 0;
};

class I_B : public I_A
{
    virtual void bar() = 0;
};

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

class B: public A, public I_B
{
    void bar() override {}
};

B b;  ///< Compiler says it is abstract
Jonny Schubert
  • 1,393
  • 2
  • 18
  • 39
  • Is there an error message that goes with this? – Carlos Aug 12 '20 at 08:18
  • The buzzword would be "diamond problem". Related/dupe: [How does virtual inheritance solve the “diamond” (multiple inheritance) ambiguity?](https://stackoverflow.com/questions/2659116/how-does-virtual-inheritance-solve-the-diamond-multiple-inheritance-ambiguit). In general, you inherit `I_A` twice and you have a definition of `A::I_A::foo()`, but not a definition of `I_B::I_A::foo()`. – Yksisarvinen Aug 12 '20 at 08:18
  • @Yksisarvinen You are right diamond describes the problem I run into. On my paper I drew it slightly different so I didn't realise. – Jonny Schubert Aug 12 '20 at 08:20

1 Answers1

0

Looks like B inherits from A, which inherits from I_B, which inherits from I_A. B also inherits from I_B, which inherits from I_A.

The diamond inheritance problem is the keyword to search, since you're inheriting the same thing two different ways. Stroustrup mentions this along with examples for what to do.

Carlos
  • 5,991
  • 6
  • 43
  • 82