Re: [boost] Is there any interest in a library that provides cont ainers with virtual destructors?

----Original Message---- From: Victor A. Wagner Jr. [mailto:vawjr@rudbek.com] Sent: 26 October 2005 05:36 To: boost@lists.boost.org; boost@lists.boost.org 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; delete p; That invokes undefined behaviour unless virtual_vector<person> has a virtual destructor. 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@Pitechnology.com Pi Technology, Milton Hall, Ely Road, Milton, Cambridge, CB4 6WZ, ENGLAND Tel: +44 (0)1223 441434
participants (1)
-
Martin Bonner