[LC++]STL Iterators and integers

Chris Vine chris at cvine.freeserve.co.uk
Fri Aug 10 08:18:04 UTC 2001


On Tuesday 07 August 2001 10:30, Grzegorz Mazur wrote:

[snip]

> 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.
>

I know I am going against fashion, but I have never really thought that was a 
good reason for not deriving from the STL container classes.  If you are 
addressing an object of a non-polymorphic class through a base class pointer, 
your program will be in trouble long before you reach the destructor.  You 
will have been calling the wrong methods for the class from the outset.

The fact that deleting it by means of a base class pointer will give 
undefined behaviour will be the least of your worries.  (And of course, if 
the object is constructed on the stack rather than the heap, correct 
destruction is guaranteed in any event -- but the program will still give the 
wrong results).

Chris.



More information about the tuxCPProgramming mailing list