Re: inheriting from std::vector bad practice?
On Apr 3, 4:52 pm, "Leigh Johnston" <le...@i42.co.uk> wrote:
"Alf P. Steinbach" <al...@start.no> wrote in
messagenews:hp7o1h$uun$1@news.eternal-september.org...
[...]
Seehttp://www.i42.co.uk/stuff/mutable_set.htmalso (something I wrote
but deriving from map/multimap instead of vector).
The only time it is unwise to use public inheritance is if
your class invariant consists of more than just vector's
invariant in which case it might be possible to break your
class's invariant by calling the vector's member functions,
but I don't believe this is the case in your example (i.e.
you are simply performing interface augmentation).
Regretfully, that's true in theory, but not in practice.
Sorry, that's bullshit. Proper design involves much more.
It's possible to disagree over what constitutes a good
design and whether something constitutes good design, but in
this case it's about the opposite, a technique that's almost
universally recognized as Bad(TM), so, no discussion.
Sorry but that's bullshit. Interface augmentation is a
perfectly valid design practice.
From the design point of view, you're completely right.
Practically speaking, however, it doesn't work out that way.
Trying to derive from a class only works out in practice if the
class was actually designed to be a base class, with few
exceptions. And while this case *is* very close to qualifying
as an exception, there's no real reason for making it one. As
someone else pointed out, the simplest and safest solution is to
follow the model in the STL, and use a free function for the
extended interface. (But depending on the context, I generally
wouldn't refuse such code using inheritance in a code review.
IMHO, the only real risk is deleting through a pointer to the
base, and in general, you shouldn't be allocating std::vector
dynamically anyway.)
--
James Kanze