[LC++]STL Iterators and integers

Davide Bolcioni 9odb6x3rfr001 at sneakemail.com
Wed Aug 8 08:20:04 UTC 2001


Grzegorz Mazur mazur at chemia.uj.edu.pl [mltuxcpp/linux-cpp list] wrote:
[cut]

>>Perhaps you're right, but I'm not convinced of this.
>>If a vector with extra functionality is what you
>>want, then I don't see why you shouldn't do this.
>>Also, the C++ programming books I have,
>>"Thinking in C++", volumes I and II, by Bruce
>>Eckel, say that  inheriting from STL classes _is_
>>often a good idea as it means you can use STL
>>algorithms on the derived classes.  Perhaps there
>>are good reasons not to inherit a vector, but I
>>haven't come across any yet.
>>
> 
> There's one important reason to avoid deriving classes from STL containers
> - AFAIK no STL container defines virtual destructor. This basically means
> that you can't ensure proper destruction of such a derived container.
> Unfortunately, I haven't read Thinking in C++, so I can't really comment
> on that.
 

class DerivedVector: public std::vector { ... }

DerivedVector * dv = new DerivedVector; // ...
std::vector * vp = dvp;

delete vp; //  ~DerivedVector() is not called, just ~vector().

The problem is obvious in the above code, but is much less obvious in real

code where new and delete are far away ... and to my knowledge, there is
no way to guard against it - except refusing to derive from a class 
which does not define a virtual destructor except in very controlled
circumstances (where you're sure the above scenario does not occur, or
you're sure the derived destuctor has really nothing to do and never
will).

[cut]

Davide Bolcioni
-- 
There is no place like /home.




More information about the tuxCPProgramming mailing list