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/ ---------------------------------------------------------------------
On Wednesday, June 5, 2002, at 09:30 AM, Bobby Thomale wrote:
class MyBase : private boost::noncopyable { public: MyBase (); virtual ~MyBase (); };
class A : public MyBase { public: A (); virtual ~A(); };
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?
All three destructors get called. Loosely speaking, ~MyBase calls ~noncopyable. The only time that the non-virtual destructor in noncopyable would matter is if you deleted an object of class A using a noncopyable*, but that's not going to happen. -- Darin
on 6/5/02 12:49 PM, Darin Adler at darin@bentspoon.com wrote:
All three destructors get called. Loosely speaking, ~MyBase calls ~noncopyable.
The only time that the non-virtual destructor in noncopyable would matter is if you deleted an object of class A using a noncopyable*, but that's not going to happen.
A-ha. That makes total sense to me. Thanks! :-) -- Bobby --------------------------------------------------------------------- Bobby Thomale Senior Software Developer Inoveon Corporation http://www.inoveon.com/ ---------------------------------------------------------------------
participants (2)
-
Bobby Thomale
-
Darin Adler