 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Siemel Naran Guest
|
Posted: Thu Jun 26, 2003 2:23 pm Post subject: Re: Deriving from concrete classes |
|
|
"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> wrote in message
| Quote: | Well, of course you could derive from an existing abstract class to
extend
the hierarchy. But imagine that one of the leaf classes offers almost
exactly what you need, except for a little thing you would just like
to |
add.
| Quote: | In that case, it would be convenient to derive from this concrete
class |
and
| Quote: | just override one of the virtual functions to add this little extra
something. However, if you derive from another abstract class, you
have to
implemement everything the concrete class offers yourself.
|
In my experience, a very compelling argument to ignore the rule that we
ought to derive only from abstract classes.
Sometimes, several virtual functions are related, and if we override one
we
should override all of them. If we derive from concrete classes there
is a
possibility we may forget to override some virtual functions. However,
perhaps this problem is better solved through either: reading the
documentation, writing an extensive test driver, factoring the code so
that
there is only one function to override (not a whole group of functions).
| Quote: | Of cource you can use a trick: Do not derive from the concrete class,
but
implement your class in terms of the concrete one, by containment or
even
better by private inheritance (so you can even redefine virtual
functions). |
Not recommended. Too much typing, plus the implicit conversion problem
Francis pointed out.
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Thu Jun 26, 2003 7:10 pm Post subject: Re: Deriving from concrete classes |
|
|
[email]pdimov (AT) mmltd (DOT) net[/email] (Peter Dimov) wrote in message
news:<7dc3b1ea.0306240311.14e01518 (AT) posting (DOT) google.com>...
| Quote: | kanze (AT) gabi-soft (DOT) fr wrote in message
news:<d6652001.0306230631.11667e05 (AT) posting (DOT) google.com>...
Again: what about the template pattern. The base class has a
concrete implementation, which can be customized in one or a few
very constrained ways; customization is through derivation. If you
have reasonable defaults for all of the customizations, for example,
what is the problem with providing them? And if you provide them,
the base class becomes concrete. It's not a particularly frequent
pattern, at least in my code, but I certainly wouldn't want to see
it banned, either, on the grounds that it involves inheriting from a
concrete class.
It is much easier to explain inheritance from an abstract base: you
should inherit if and only if your class needs to implement the
interface represented by the abstract base.
Inheritance from a concrete class is much harder to explain or
justify; an inexperienced programmer can't easily tell the good uses
from the bad, and an experienced programmer sometimes needs to look
several moves ahead to foresee the potential problems.
So I'd leave the virtual functions pure in your example, even if I had
good defaults. Of course this has its drawbacks, too.
|
I would certainly teach inhertance starting with abstract base classes.
But I don't work in a school; I expect my collegues to already know what
inheritance is, and to be familiar with the concept of interfaces.
For the rest, I've found that a reference to the design pattern has
generally been sufficient. No one in the places I've worked would dare
admit to not being familiar with the design patterns in the Gang of
Four's book:-).
On the other hand, the case is relatively rare. Generally speaking, in
my more recent work, I will use delegates rather than the template
pattern. So the issue doesn't come up that often. (And I can't think
of any other case off hand where you might want to inherit from a
concrete class.)
--
James Kanze GABI Software
mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter
Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45
16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Dimov Guest
|
Posted: Fri Jun 27, 2003 2:07 pm Post subject: Re: Deriving from concrete classes |
|
|
[email]kanze (AT) gabi-soft (DOT) fr[/email] wrote in message news:<d6652001.0306260147.3aac7ac6 (AT) posting (DOT) google.com>...
| Quote: | pdimov (AT) mmltd (DOT) net (Peter Dimov) wrote in message
news:<7dc3b1ea.0306240311.14e01518 (AT) posting (DOT) google.com>...
So I'd leave the virtual functions pure in your example, even if I had
good defaults. Of course this has its drawbacks, too. :-)
I would certainly teach inhertance starting with abstract base classes.
But I don't work in a school; I expect my collegues to already know what
inheritance is, and to be familiar with the concept of interfaces.
|
I have (empirically) found that actively avoiding inheritance from
concrete classes pays off regardless of skill. It often leads to
better designs, but that aside, one very practical reason for the
guideline is that C++ doesn't have "override" to help prevent mistakes
like:
struct X
{
virtual void f() const;
};
struct Y: X
{
virtual void f();
};
where Y::f is intended to override X::f but does not.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Siemel Naran Guest
|
Posted: Sat Jun 28, 2003 11:11 pm Post subject: Re: Deriving from concrete classes |
|
|
"Peter Dimov" <pdimov (AT) mmltd (DOT) net> wrote in message
| Quote: | I have (empirically) found that actively avoiding inheritance from
concrete classes pays off regardless of skill. It often leads to
better designs, but that aside, one very practical reason for the
|
Why? Can you elaborate?
--
+++++++++++
Siemel Naran
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|