Fog Creek Software
Discussion Board




Anyone use Multiple Inheritance?

Has anyone here ever used Mutliple Inheritance in a real world project?

What language did you use? What was it useful for?

I have never seen multiple inheritance used on any of the C++ projects I have worked on, and at most places the coding standards pretty much forbade its use.

But surely someone out there is/has used it?

Not Telling
Friday, May 16, 2003

I've made limited use of it in C++, to simulate interfaces, i.e., no data members, only methods.

Worked like a charm.

Paulo Caetano
Friday, May 16, 2003

Sorry, I should have been clearer. I am talking about full blown implementation inheritance (data + methods), not interface inheritance, which of course is the commonly used form of multiple inheritance in C++.

Not Telling
Friday, May 16, 2003

IIRC, the C++ iostream libraries use multiple inheritance.

I also recall reading on a 'reliable' source (slashdot) that the author regretted his choice of using MI.

Personally, I'm perfectly happy with being limited to single-inheritance plus interfaces.

Mike Swieton
Friday, May 16, 2003

Yes, we use it all the time in a fairly large scale network simulator, especially combined with templating.

It does just what it says on the tin!

For example, we might have a particular cellular telephone interface (not a coding interface, but say a radio connection between a handset and a base station). This is a radio connection, which can be templated based on the channel characteristics and direction (1st inheritance), but at the level we are looking at, we consider it as an instantiation of a certain protocol (2nd inheritance). It would take quite a long time to explain in detail, but it does work very well.

It isn't that hard to do and keep track of really.

That said, I'm getting very jaded with C++. I've been doing a lot of ruby lately ( www.ruby-lang.org ). I find that ruby's single inheritance mechanism plus mixins of modules provides a really powerful inheritance framework

best regards, treefrog

treefrog
Friday, May 16, 2003

I use MI all the time. I find the trick to having happiness result is a number of small, specific lightweight mixins that you blend together. You keep the inheritance trees relatively shallow.

In some projects more than half the useful classes I create end up being blended classes like this - I like the freedom to have, say, Serialisable, be something I can go around painting into objects, rather than have it be a property of some root class that All Other Classes Must Descend From.

Most places do ban it, but if you look closely at their code you find they don't actually do much inheritance at all. Scary. It's all based on this religious belief that they once heard someone say that they'd heard it said that MI is dangerous. Not because they've anyone around who's ever tried it.

Katie Lucas
Friday, May 16, 2003

take a look at MS's ATL -- MI is used all over the place (and works quite nicely, too)

Bill
Friday, May 16, 2003

Yep, +1 for the "mixin" philosophy. I saw it used with great success in ATL and adopted the usage model. Now I'm in C#, so no chance to do it. Oh well... :-p

Brad Wilson (dotnetguy.techieswithcats.com)
Friday, May 16, 2003

Multiple Inheritance works well in languages like Eiffel.

Languages without it (e.g. Java) disparage it in order to try and hide their own shortcomings.

Take a look at some of the libraries at:

http://www.gobosoft.com/

David Basil Wildgoose
Friday, May 16, 2003

MI is a useful idiom but comes at a price. Some examples of the usefulness of MI:
1. Combining the functionality of orthogonal object hierarchies.
2. Mixings as has been described (to an extent this is a special case of the previous item).
3. It has been shown that exception hierarchies are best done as a "forest" rather than a single tree.

Basically, if it makes sense to have multiple interface inheritance then it also makes sense to have multiple implementation inheritance.

As I said, this feature does carry a price tag, which is why some languages leave it out (and some developers don't use it with languages that have it). For example, there is the issue of inheriting the same implementation several time through several base-classes (C++ virtual inheritance et al).

You also have alternatives to MI such is the delegate design pattern. However, there is no doubt that using MI in a language that supports it is significantly easier than the alternatives.

Dan Shappir
Friday, May 16, 2003

In C++, I use it for mix-ins.

In languages that have more elegant support for mix-ins (e.g. Ruby), I have never seen a need fo MI.

raindog
Friday, May 16, 2003

I've used MI in C++, again to merge orthogonal heirarchies. Honestly I wouldn't have designed the heirarchy that way, but since it come up (it was the intersection of an existing heirarchy with one that matched some new buisness rules), MI was the easiest (and most understandable) way to do the job.

I don't think I've ever designed MI into a system deliberately - if I did, I'd go with shallow mixin heirachies, rather than something large.

Michael Kohne
Monday, May 19, 2003

class Money : Parent, Grandparent
{
  friend Me;

  // don't want taxman to see this
private:
  unsigned long howMuchDoIGet() const throw (TheyAreNotDeadYet);
};

Seems so.


Monday, May 19, 2003

MI can also be used quite successfully in GUI/widget libraries -- the qualities of being scrollable and paintable, for instance, may be the same across many widgets (who differ in other respects).

Steven C.
Monday, May 19, 2003

*  Recent Topics

*  Fog Creek Home