Once and for all I want to clearify this somewhat subjective and argumentative area of programming.
Multiple inheritnace
In my current working enviornment I have C++ developers and C# developers which come from totaly different worlds and thus have different opinions on programming layout.
Now being a C# and Java developer myself I’ve never come to the state where I actually needed to use Multiple inheritance, but C++ developers around me tend to through out comments like “That would be a perfect way to use Multiple inheritance”
Of course I tend to dissagree.
My question
In what scenario would multiple inheritance be a better or easier way to solve a problem than use of Interfaces and just simple inheritance?
And can you always solve the multiple inheritance benefits by using ie member variables instead?
Except that you seem to be proposing to use multiple inheritance.
Inheritance has different uses. Public inheritance specifies an IS-A relationship, and inheritance with any access modifiers can provide behavior to the child class, which can then be overridden. In C# and Java (which I’m more familiar with), interfaces provide the IS-A interface without behavior. (In C++, you can do an interface by defining a class with no data members and all functions pure virtual.)
So, if you provide an interface, and put in a member variable to provide behavior, you’re doing full C++-style inheritance. You’re just breaking it up into components and taking more trouble to do it. Describing this as solving the multiple inheritance problem is disingenuous, since doing that with two interfaces and two member variables is multiple inheritance. It gets even more awkward if you’re going to modify the behavior in a polymorphic fashion, since either you need parallel inheritance hierarchies or a more complicated dispatch to the member variable.
There are problems with C++ multiple inheritance, true, but there are also uses that don’t present problems. The first good use I saw was “mix-ins”: small classes to add certain behaviors. There’s plenty of interfaces to these in Java, but you’re expected to handle the behavior by yourself.
If you’re interested in learning more about multiple inheritance, I’d suggest trying a language, like Common Lisp, where multiple inheritance is routinely used and nobody has problems with it.