
----- Original Message ----- From: "Jeff Garland" <jeff@crystalclearsoftware.com> To: <boost@lists.boost.org> Sent: Saturday, February 26, 2005 9:55 PM Subject: Re: [boost] Re: Querying usefulness of a macro for inheriting fromSTLcontainers
On Fri, 25 Feb 2005 18:26:22 +0000, Jonathan Wakely wrote
christopher diggins wrote: I think publicly inheriting from a class that isn't designed for inheritance is a bad idea. There are type safety problems, non-virtual destructors, slicing etc.
Hi Daniel thanks for the help and suggestions ( and Kevin and Jonathan as well),
I have heard here and there, inheriting from STL container is a "bad idea", but I don't specifically know all the reasons why. I am not an expert in this area (heck or any part of C++ for that matter). Isn't it only bad if someone tries to delete the object using a base class pointer?
That's one reason, yes.
(btw, this is Item 35 in Sutter and Alexandrescu's C++ Coding Standards)
At the risk of going against the entrenched dogma here, I'd suggest there are significant cases where this is a good and useful thing to do. I think the original guidance came from Scott Meyers Effective C++ #14 -- make destructors virtual in base classes. The problem I have with this is that I find there are plenty of situations where I might want to add some convenience functions to an stl container in which the subclass has a trivial destructor (eg: no allocated resources)
I am a little confused here, does MySubClass below have a "trivial" destructor? MySubClass : MyBaseClass { int x; }; In other if I write: MyBaseClass* p = new MySubClass; delete p; Do I leak the memory for integer x? And then what happens here: Baz { ~Baz() { cout << "destroying baz"; } }; MySubClass : MyBaseClass { Baz x; }; MyBaseClass* p = new MySubClass; delete p; Does the baz get destroyed? I always assumed it should.
If you want to inherit from, say, vector, do you _really_ want _all_ its public member functions?
Yes, I frequently do.
Jeff
So Jeff, would you find a set of macros for facilitating the inheritance of STL containers useful? Christopher Diggins Object Oriented Template Library (OOTL) http://www.ootl.org