views:

309

answers:

5

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?

+1  A: 

Have a look at this SO topic: Should C# include multiple inheritance?

Lucero
+3  A: 
AlexKR
A: 

I haven't used multiple inheritance a lot, but sometimes, it comes handy because it just performs well and reduces the maintenance costs. C++ FAQ Lite answers have some good scenerios.

JP
+1  A: 

Having used two languages extensively where one has (Python) multiple inheritance and one doesn't (C#) I can honestly say that I have never used or had the need for MI.

I tend to prefer interfaces + composition in most cases over both normal inheritance and/or multiple inheritance. Sure there are some cases where you really need inheritance, especially in C# - but MI? Never.

Edit: It would be easier to answer if you could show an example where your C++ programmers advocated MI and you didn't.

Edit: Thinking about it a bit more I realized that the difference between a statically typed language like C# or C++ and a duck-typed language like Python might be a good reason I never had to use MI in Python, just due to it's dynamic nature. But still, I have not had the need for it in C# ever.

thr
+3  A: 

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.

David Thornley