home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!news.centerline.com!matt
- From: matt@centerline.com (Matt Landau)
- Newsgroups: comp.lang.c++
- Subject: Re: Multiple Inheritance - Necessary?
- Date: 20 Nov 1992 20:55:38 GMT
- Organization: CenterLine Software, Inc.
- Lines: 66
- Message-ID: <1ejjcaINN1m5@armory.centerline.com>
- References: <By17An.4uH@usenet.ucs.indiana.edu>
- NNTP-Posting-Host: 140.239.1.32
- Keywords: c++ inheritance
-
- In <By17An.4uH@usenet.ucs.indiana.edu> ezachris@iroquois.ucs.indiana.edu writes:
- >A friend of mine who programs in Objective-C argues that multiple
- >inheritance is unnecessary and complicated.
- >
- >My Objective-C friend would make a new object "Pencil-with-eraser" that
- >would declare two objects inside it.
- >
- > PENCIL-WITH-ERASER
- > Eraser TheEraser(...);
- > Pencil ThePencil(...);
- >
- >I think this sounds like hogwash. What do you think?
-
- There are basically two ways you can combine the features of existing
- classes: composition and inheritance. Your friend's example above is
- an example where combining classes by composition works perfectly well,
- as long as you don't mind the overhead of having to reimplement many
- member functions of both Pencil and Eraser in your new composed class,
- delegating them back to the component parts.
-
- There are other uses of MI for which composition just doesn't provide
- the right kind of abstraction. Consider a class "Collection" that lets
- you insert, remove, and search for elements. Objects that are part of
- a collection must have certain attributes that comprise "collectability";
- in C++ these attributes are often expressed as virtual member functions
- used by the Collection class itself when manipulating members of the
- collection.
-
- One approach to building such a system involves declaring an abstract
- base class which we might call Collectable, whose purpose is to declare
- the required virtual functions. In fact, it would declare that as pure
- virtuals, so that derived classes are forced to provide an implementation
- for each of them.
-
- To create a collection of pencils, one would want a class that combined
- the attributes of a pencil with the characteristic of collectability; in
- C++, one could express this using multiple inheritance:
-
- class CollectablePencil : public Pencil, public Collectable
- {
- public:
- CollectablePencil(args) : Pencil(args) { }
- virtual ~CollectablePencil(void);
-
- ... implement Collectable's pure virtuals ...
- };
-
- Now CollectablePencil can be used in any context where Pencil can be
- used, *and* can become an element of a Collection. Similarly, to build
- a herd of elephants, one might start with:
-
- class Elephant : public Pachyderm, public Collectable
- {
- ...
- };
-
- I'm not saying this is the BEST way to deal with collections, but absent
- templates (which most compilers are just starting to support), it is an
- adequate way, and it's used right now by a number of widely available class
- libraries. It also happens to be a technique that really *requires* MI,
- and cannot be done with composition instead.
-
- Personally, I find C++'s version of multiple inheritance overcomplicated,
- confusing, and extremely fragile - basically more trouble than it's worth.
- In my own programming, I try not to use it for anything *except* this sort
- of attribute-based mixins.
-