Boost logo

Boost :

From: Victor A. Wagner Jr. (vawjr_at_[hidden])
Date: 2005-10-26 09:16:25


At 02:51 2005-10-26, Martin Bonner wrote:
>----Original Message----
>From: Victor A. Wagner Jr. [mailto:vawjr_at_[hidden]]
>Sent: 26 October 2005 05:36
>To: boost_at_[hidden]; boost_at_[hidden]
>Subject: Re: [boost] Is there any interest in a library that provides
>containers with virtual destructors?
>
> > At 10:04 2005-10-25, Andy Tompkins wrote:
> >> I have read a few times that people sometimes wish that std::vector
> >> (or other containers) had a virtual destructor.
>[snip]
> >> This allows one to write something like:
> >>
> >> struct person {
> >> string first_name;
> >> string last_name;
> >> };
> >> class people : public virtual_vector<person>
> >> {
> >> public:
> >> //extra methods
> >> };
> >>
> >> Is this useful?
> >
> > unless I mis-read (or mis-understood...far more likely) a lot of
> > books on how C++ works, I don't believe there is any reason for what
> > _you've_ shown to need a virtual destructor.
>
>You have mis-read or mis-understood (or you need better books). Consider:
> virtual_vector<person>* p = new people;

I guees I wasn't clear.. I was suggesting that virtual_vector has no
need for existence.

> delete p;
>That invokes undefined behaviour unless virtual_vector<person> has a virtual
>destructor.

are you suggesting that if it were class people: public
std::vector<person> {.....}; as above the same would be true?
not "in practice", "in theory".

>In *practise*, you will (probably) get away with it. The most *likely*
>behaviour is that virtual_vector<>::~virtual_vector() will be called,
>without calling people::~people(). ... but that doesn't alter the fact that
>it *is* undefined behaviour, which is not good.
>
>
>
>--
>Martin Bonner
>Martin.Bonner_at_[hidden]
>Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ,
>ENGLAND Tel: +44 (0)1223 441434
>_______________________________________________
>Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Victor A. Wagner Jr. http://rudbek.com
The five most dangerous words in the English language:
               "There oughta be a law"


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk