views:

187

answers:

6

Are there any scenarios in which multiple inheritence is necessary? Please provide examples.

A: 

When the developer is lazy and doesn't want to produce a better class design that wouldn't need multiple inheritance ;-) I primarily code in C# and have never found a scenario that caused me to curse their design of only allowing single inheritance.

That being said, being able to implement multiple interfaces is really handy.

Joel Martinez
But you have most likely used multiple interfaces (Or used both inheritance and interfaces). But that would be multiple inheritance in c++, because c++ don't have interfaces as a independent concept. (A c++ developer just think about a interface as a class with no implemented methods).
Martin Tilsted
+4  A: 

If you want to simulate something like Interfaces that other OOP languages have, then you'll need to use multiple inheritence.

FigBug
+1, exactly what I was coming to say
Jess
Except that interfaces are a restricted special case of the more generic multiple inheritance (e.g. a pure virtual abstract base class).
Rob K
+1  A: 

With pure abstract classes you can have interfaces, like you do in Java or C#

Marco Mustapic
A: 

There are no scenarios where anything more than a Turing machine is necessary.

John M
+1  A: 

Anything which "requires" inheritance, and which you want to use more than once on a single class, or combine in a single class, "requires" multiple inheritance.

Common C++ idioms which use inheritance include:

So, if you want a single class implementing multiple interfaces then you need multiple inheritance. If you want a policy-based iterator type, then you need multiple inheritance (once for the policy, and once for the traits).

I put "requires" in snigger quotes because obviously in all cases you can write a program having the same output, and which does not use multiple inheritance (the "Turing complete is all we care about" argument). However, Turing completeness is not all we care about, and we usually have goals in writing software beyond Turing's concept of a program with input and output. We care what the source looks like. Hence, "necessary".

Steve Jessop
A: 

When building COM objects.

Goz