Re: Deriving from concrete types
Bernhard Jungk wrote:
2. The only reason for derivation is to allow
polymorphic use (treating a derived
class as a base class.)
This isn't a dogma of the C++ community. It's a matter of good
OOD. Public inheritance is provided to allow polymorphic use.
The derived class has to be replaceable with the base class.
That depends. I think your confusing derivation and
inheritance. Or whatever you want to call them; the confusion
often occurs, because there is no standard vocabulary for
distinguishing between:
-- an essential OO design pattern, in which a number of derived
types can be used indifferently in place of the base type,
and
-- a C++ implementation technique, which is often used to
implement the design pattern, but of course, can be used to
implement other patterns as well.
If you are using C++ inheritance to implement OO derivation (or
is it C++ derivation to implement OO inheritance---as I said,
there is no established vocabulary for making the distinction),
then the base class very definitly must be designed
intentionally to be a base class for OO derivation. Which
includes such things as having a virtual destructor. And which
std::vector is not. If you are using C++ inheritance for some
other reason, the other rules may apply; std::iterator is
certainly designed to be a base class in the C++ sense, even if
it has no virtual functions.
My experience has been that users tend to be able to cope with
this pretty well, as long as you've documented the role your
class was designed to fulfil. I've never heard of anyone trying
to delete an iterator through an std::iterator*, for example.
(For that matter, I don't think I've ever heard of anyone even
declaring an std::iterator*.)
If the base class isn't designed to be derivable, it shouldn't
be done with public inheritance.
You see. You've mixed the two. Inheritance is a C++
implementation technique; derivation is a design concept. If
the reason you are using inheritance is to implement derivation,
then you should ensure that your class is designed with
derivation in mind. If you are using it for some other reason,
you should ensure that your class meets the requirements for
whatever the reason.
Public inheritance defines a "is a" relationship
Public derivation defines a "is a" relationship. Public
inheritance is the obvious way to implement it. So much so that
I would say that public derivation implies public inheritance.
But it's not an if and only if relationship.
--
James Kanze GABI Software
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]