Re: Inherit from vector, encapsulate, or not bother?
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in news:fu55l2$gmc$1
@news.datemas.de:
2. Derive a class from vector (from googling people seem to think this
is a bad idea)
It depends. If your new container is not going to "specialise" the
vector (so to speak), then derive away. Inheritance is a mechanism
and the usefulness of it is in the eye of the beholder.
To expand a bit on this. The "problem" with inheriting from std::vector is
that it doesn't have a virtual interface and especially it doesn't have a
virtual destructor. If you don't add any additional state information,
then it doesn't matter much, but if you do add more variables and maybe
tweak a method or two, then you are in a situation where you can
"accidentally" pass your new vector to a routine which expects a
std::vector and things can go really bad. For example, the routine swaps
your vector with a new one in the attempt to implement strong type safety.
Now your new vector gets destroyed with the std destructor and you leak
memory, fault or something more subtle. Bad news. On the otherhand, if
you are just adding some functions and just want a way to keep them all in
one spot, then you are probably ok. Delegation and private inheritance
solve this by not letting you pass your new collection as a std::vector.
joe