RE: [Boost-Users] boost::noncopyable usage question
This is more of a C++ question than a boost one (which is why I can answer it;). Your deletes are fine. The only way this would cause a problem is to delete through a boost::noncopyable pointer (in which case everything derived from it would leak). I'm guessing the author, Dave Abrahams, didn't think anyone was going to declare a noncopyable*, so decided to pass on that little bit of extra overhead involved in declaring a destructor virtual. Or it could be a mistake. Dave has posted to this list at least once... So tell us, did you mean to leave it non-virtual? --Mark Storer Software Engineer Cardiff Software #include <disclaimer> typedef std::disclaimer<Cardiff> Discard; -----Original Message----- From: Bobby Thomale [mailto:bthomale@mac.com] Sent: Wednesday, June 05, 2002 9:30 AM To: Boost Users List Subject: [Boost-Users] boost::noncopyable usage question I have a simple question about the usage of boost::noncopyable. I think I know the answer, but I am not sure. The noncopyable class defines a non-virtual destructor. Generally speaking, overriding a non-virtual destructor is considered a "bad thing" but in certain cases (like, I am guessing, this one) it is okay and works like you'd expect. Here's what I am worried about: Let's say I have a base class of an inheritance tree that I want to be non-copyable. So I do this: class MyBase : private boost::noncopyable { public: MyBase (); virtual ~MyBase (); ... }; Then I derive from it class A: class A : public MyBase { public: A (); virtual ~A(); }; and define a destructor for class A, too. Does this do what I would expect, ie whether I delete a MyBase* or an A*, it calls the destructors for both A and MyBase like it is supposed to? I suppose the trivial, empty noncopyable destructor never gets called (which is okay) whether I delete a MyBase* or an A*, correct? (Just curious...) -- Bobby --------------------------------------------------------------------- Bobby Thomale Senior Software Developer Inoveon Corporation http://www.inoveon.com/ --------------------------------------------------------------------- Info: http://www.boost.org Wiki: http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl Unsubscribe: mailto:boost-users-unsubscribe@yahoogroups.com Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
on 6/5/02 12:49 PM, Mark Storer at mstorer@cardiff.com wrote:
This is more of a C++ question than a boost one (which is why I can answer it;).
:-) Yep. Thanks!
I'm guessing the author, Dave Abrahams, didn't think anyone was going to declare a noncopyable*, so decided to pass on that little bit of extra overhead involved in declaring a destructor virtual. Or it could be a mistake.
I am sure he meant to leave it non-virtual. There's more than just a little calling overhead involved - there's also per-object space overhead. If it was virtual, then any class derived from it would have a vtable, which is often not what you want at all. By making it non-virtual, it gives the user the choice of using it to create either a polymorphic or non-polymorphic class, depending on your needs. Furthermore, by making the destructor protected, I suspect that this would cause any attempt to delete a noncopyable* to generate an error? Is that right? I just wanted to make sure that this would work the way I thought it did if I start using it in my non-copyable base classes. (Mainly I was curious about whether this would keep the noncopyable destructor from being called - apparently the answer is no, which is good.) It's very clever - I have seen other people encapsulate the boilerplate non-copyable stuff in wacky macros and stuff before, but the noncopyable base class is _much_ more elegant, and so simple! -- Bobby --------------------------------------------------------------------- Bobby Thomale Senior Software Developer Inoveon Corporation http://www.inoveon.com/ ---------------------------------------------------------------------
You usually use private inheritance for boost::noncopyable, that should prevent users from casting to boost::noncopyable. The boost::noncopyable destructor is also protected and *can* only be called by a derived class. class X : private boost::noncopyable { public: X() { } virtual ~X() { } }; int main() { boost::noncopyable* x = reinterpret_castboost::noncopyable*(new X()); delete x; } Should give a compile time error like cannot access protected member declared in class 'boost::noncopyable' Daniel
"Mark Storer"
I'm guessing the author, Dave Abrahams, didn't think anyone was going to declare a noncopyable*, so decided to pass on that little bit of extra overhead involved in declaring a destructor virtual. Or it could be a mistake.
Dave has posted to this list at least once... So tell us, did you mean to leave it non-virtual?
Absolutely, totally.
participants (4)
-
Bobby Thomale
-
Daniel van der Zee
-
David Abrahams
-
Mark Storer