3

Is multiple inheritance with interface class a good design practice?

It seems to work and makes running gtest unittest suite convenient. So in example below, is multiple inheritance of class B a bad design practice?

#include <stdio.h>

class BInterface {
 public:
    virtual ~BInterface() {}
    virtual void idb() = 0;
};

class A {
 public:
    A() {}
    void ida() {printf("I am A\n");}
};

class B : public A, public BInterface {  // ###########  Is this OK?  ############
 public:
    B() {}
    void idb() {printf("I am B\n");}
};

int main() {

    B BObject;

    BInterface* BI = &BObject;

    BI->idb();

    return 0;
}

Edit: I am OK with just inheritance of interface class but is multiple inheritance in such case a bad design, considering that most of code experts recommend not to use multiple inheritance.

user1135541
  • 1,781
  • 3
  • 19
  • 41
  • I don't see anything wrong with a single level of inheritance. Don't go too deep though, or it gets hard to follow which class is handling what responsibilities. – Mark Ransom Jun 04 '14 at 18:39
  • re "in example below, is multiple inheritance of class B", there is no multiple inheritance of class B in the example as it is as of me writing this comment. generally it's OK to inherit an interface class multiple times. but you only want to inherit an implementation class once. – Cheers and hth. - Alf Jun 04 '14 at 18:46
  • 1
    If it helps you uncouple your design, it can be a good idea. Still, be sure it does so. Otherwise, abstain: `"Any problem in programming can be solved with an extra layer of abstraction, with the exception of too many layers of abstraction."` Anyway, strictly speaking your `BInterface` is not a pure interface. – Deduplicator Jun 04 '14 at 18:47
  • Refer this question: http://stackoverflow.com/questions/56867/interface-vs-base-class?rq=1 – excray Jun 04 '14 at 18:49
  • @excray: Keep in mind though that it is answered mainly from a MI is impossible in my language standpoint. – Deduplicator Jun 04 '14 at 18:50
  • Deduplicator, why is it not a pure interface? Actually, it really helps me by allowing decoupling for unit testing and decoupling templated objects. – user1135541 Jun 04 '14 at 18:56

2 Answers2

7

It's technically OK to inherit interface classes. After all that's what they're designed for. Whether it makes design level sense is another matter, not possible to decide without considering the particular design.

Within C++ there are some advantages of making interface inheritance virtual, in particular that you can inherit in an implementation, but it also can have a slight cost in efficiency.

Microsoft's COM is an example of an interface-based architecture that does not use virtual inheritance of interface. Use of virtual would have complicated1 the C view of a COM class. But for purely all-within-C++ code there is (hopefully) no such consideration.


Regarding

in example below, is multiple inheritance of class B a bad design practice?

… there is no multiple inheritance of class B in the example as it is as of me writing this.

Generally it's OK to inherit an interface class multiple times, but you only want to inherit an implementation class once.


1) By not using virtual inheritance, or an equivalent scheme, a COM object may have more than one base class sub-object of type IUnknown, and this complicates the COM notion of object identity. Essentially it's resolved by providing a member function where client code can ask for an interface pointer of a specified interface, where IUnknown is required to be treated specially, always returning a pointer to identity-defining sub-object. Similar considerations of identity may apply for ordinary C++ interface inheritance.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

Concerning design, there is nothing bad or particularly good about multiple inheritance with interface bases. If it is the most simple way to do what you need to do, do it.

The same goes for more complicated multiple inheritance. In C++, there is no need to avoid it, but you should know what you are doing if you use it. Even diamond shaped inheritance with a virtual base can be of use, but should not be used lightly. It tends to confuse your readers.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106