FAQ 7.08 What do subsets have to do with proper inheritance?

Nothing.

Remember, the goal is to avoid breaking existing code that uses base class pointers and references. Since elements of a subset don't necessarily behave in a backward compatible way with respect to the superset, users might be surprised if they are passed an element of a subset, and all that code using base class pointers/references might break. It doesn't matter that intuition says otherwise; the fact remains that the whole subset notion is wrong often enough that it is not a reliable design principle. Consider the following two examples:

  • The set of circles is a subset of the set of all ellipses, yet circles can't deform themselves asymmetrically, which may be a member function of class Ellipse (see FAQ 8.08).

  • The set of ostriches is a subset of the set of all birds, yet ostriches cannot fly, which may be a member function of class Bird (see FAQ 8.04).

The root problem with the subsets-mean-subclasses idea is that subsets deal with values, where objects normally have mutative member functions (deform asymmetrically, fly, and so on).

Forget subsets and learn about substitutability.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net