[LC++]STL Iterators and integers

Chris Vine chris at cvine.freeserve.co.uk
Sat Aug 11 08:02:03 UTC 2001


On Friday 10 August 2001 09:02, Grzegorz Mazur wrote:

[snip]

> I hate to support politically correct and fahionable point of view, but in
> this case I have to do so. I try to put it simple: the difference is that
> destructor will be called always, for any object which was created, and
> even worse, the destructor is called implicitly, so your only hope is that
> compiler generated code which selects the right one.

[snip]

I am not sure what you are saying here, because if a destructor is called 
implicitly (say, because a local object goes out of scope) then the correct 
destructors are always called, in the correct order for all the classes from 
which the object is derived.  The compiler doesn't need to guess - it makes 
no difference what pointers you may or may not have assigned the address of 
the object to before it goes out of scope.  The problem, so far as it exists, 
is with explicit deletion through a base class pointer of an object 
constructed on free store.

But that is peripheral to the main point, which was -

> Another way to explain it is following. If you try to override a member
> function which is not virtual and then call it through a pointer to base
> class, the behaviour can (and will) be different from what you would
> (wrongly) expect, but is well-defined. If you make the same mistake with
> destructor, you have undefined behaviour at the first place where delete
> with pointer to base class is called.

This is a matter of taste.  Wrong behaviour is wrong behaviour, whether 
defined or not.  In fact, a crash would be a friend -- it would make it even 
clearer that something is wrong.  I don't think the argument that this wrong 
behaviour is better than that wrong behaviour holds much water.  However, 
everyone is entitled to their opinion!

Chris.



More information about the tuxCPProgramming mailing list