Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #10702

Re: Destruction order

From Richard Damon <news.x.richarddamon@xoxy.net>
Newsgroups comp.lang.c++
Subject Re: Destruction order
Date 2011-10-06 10:49 -0400
Organization A noiseless patient Spider
Message-ID <j6kf63$3fp$1@dont-email.me> (permalink)
References <e3bec1c1-d94d-40ed-9c3c-bde78cbf976d@y18g2000yqy.googlegroups.com>

Show all headers | View raw


On 10/3/11 12:48 PM, Christopher wrote:
> The FAQ says, "If you are relying on order dependencies in a virtual
> inheritance hierarchy, you'll need a lot more information than is in
> this FAQ."
>
> Can someone post that information here?
>
> I was under the impression that when a base class has a virtual
> destructor, that the destruction order is from most derived to base.
>
> I've got a tricky smart pointer member that is going to depend on this
> and wanted to verify that it is always guarenteed.
>
> P.S.
> On an aside, the FAQ is FAQ LITE, is there a FAQ Full Flavor?
>

The standard is very clear on the order that a class hierarchy is 
constructed, (and by the reverse order rule, destructed). The basic rule 
is that when a constructor for a class is entered, it first calls the 
constructor from base classes, in a left to right order, then the 
constructors for its member variables in deceleration order.

The one hiccup to this order is virtual base classes, as those may be 
mentioned several places in the inheritance tree, but all these 
represent a single sub-object that will only be constructed once. I 
don't know if the standard gives a precise timing of when this object is 
created, but I am fairly sure that it is guaranteed to have been created 
by at least the point where it have been by the basic rule. I suspect 
that virtual base classes will need to be created before any of the 
"normal" base classes, but I don't know it that is promised by the 
standard. Note that if you inherit from a base class that has a virtual 
base class, even if you do not specify that you inherit from that 
virtual base, your constructor is still going to need to construct that 
virtual base, it doesn't get deferred to your base that declared the 
relationship.

As has been pointed out, this (and what the FAQ is referring to) is 
virtual inheritance, "class foo: virtual pubic bar, public baz {}" not 
just a virtual destructor. A virtual destructor just make sure that the 
right destructor gets called if you delete an object through a pointer 
to a base class (without the virtual destructor, this operation causes 
undefined behavior).

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Destruction order Christopher <cpisz@austin.rr.com> - 2011-10-03 09:48 -0700
  Re: Destruction order Noah Roberts <roberts.noah@gmail.com> - 2011-10-03 09:54 -0700
  Re: Destruction order Krice <paulkp@mbnet.fi> - 2011-10-03 10:44 -0700
    Re: Destruction order Paavo Helde <myfirstname@osa.pri.ee> - 2011-10-03 13:18 -0500
    Re: Destruction order Christopher <cpisz@austin.rr.com> - 2011-10-03 11:13 -0700
      Re: Destruction order Krice <paulkp@mbnet.fi> - 2011-10-06 00:07 -0700
        Re: Destruction order Werner <werasm@gmail.com> - 2011-10-06 02:25 -0700
        Re: Destruction order Christopher <cpisz@austin.rr.com> - 2011-10-06 10:16 -0700
          Re: Destruction order Werner <werasm@gmail.com> - 2011-10-07 00:30 -0700
    Re: Destruction order Werner <werasm@gmail.com> - 2011-10-06 02:22 -0700
  Re: Destruction order Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2011-10-03 20:56 +0200
  Re: Destruction order Richard Damon <news.x.richarddamon@xoxy.net> - 2011-10-06 10:49 -0400

csiph-web