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.)
... Public inheritance is provided to allow polymorphic use.
Perhaps in the OOD paradigm, this is the only reason for
public inheritance (I'm not an expert in OOD theory.)
However, one of C++'s strengths is that it supports
multiple paradigms. And one real non-OOD use of
derivation is code re-use, where there is no expectation
of polymorphic use.
The OP's example is, to my mind, a perfect example
of derivation for code re-use: a new class which is
similar to an existing class.
By using public inheritance, the new class definition
contains only those things which distinguish it from
the existing class, thus making it clear to anyone
who reads it what is going on.
(Yes, you can use private inheritance, but then
you have to put in a "using" declaration for every
base class function and operator. Quick, how
many of you can rattle off all the member functions
and operators in std::vector without looking at
If Stroustrup had intended to create a language
which would force people to fit their code into
OOD paradigms, perhaps he would have made
it impossible to derive from a class with a non-virtual
destructor. Or made it impossible to override
Fortunately, he did not.
And C++ is all the more useful for it.
(Just think of all the one-paradigm languages out there --
have *any* of them become more than niche languages?)
-- Alan McKenney
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]