Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Order of destruction

Reply
Thread Tools

Order of destruction

 
 
Lilith
Guest
Posts: n/a
 
      04-03-2008
Does the standard define the order in which objects declared in the
same scope are destructed when those objects go out of scope? This
has a bearing on some cleanup work I want to incorporate in one of my
classes.

TIA,
Lilith
 
Reply With Quote
 
 
 
 
Martin York
Guest
Posts: n/a
 
      04-03-2008
On Apr 3, 7:43 am, Lilith <(E-Mail Removed)> wrote:
> Does the standard define the order in which objects declared in the
> same scope are destructed when those objects go out of scope?


Yes.
Reverse order of creation.
 
Reply With Quote
 
 
 
 
Lilith
Guest
Posts: n/a
 
      04-03-2008
On Thu, 3 Apr 2008 08:35:52 -0700 (PDT), Martin York
<(E-Mail Removed)> wrote:

>On Apr 3, 7:43 am, Lilith <(E-Mail Removed)> wrote:
>> Does the standard define the order in which objects declared in the
>> same scope are destructed when those objects go out of scope?


>Yes.
>Reverse order of creation.


Thank you. That makes it much easier to maintain.

--
Lilith
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-04-2008
On Apr 4, 1:22 am, Ron Natalie <(E-Mail Removed)> wrote:
> Lilith wrote:
> > Does the standard define the order in which objects declared
> > in the same scope are destructed when those objects go out
> > of scope? This has a bearing on some cleanup work I want to
> > incorporate in one of my classes.


> Scope has no meaning for object lifetime. It doesn't exist at
> runtime.


Yes and no. The concepts are definitely orthogonal, but for
historical reasons, in C++, object lifetime, or at least the
default object lifetime, is partially determined by the scope of
the object's definition.

> But, with the exception of the dynamic allocation where you
> specifically have control over when you destroy it, objects
> are always destructed in reverse order of construction.


Only within very restricted categories, and with numerous
exceptions. Reverse order is true for members of a class,
temporaries whose lifetime hasn't been extended, non-static
local variables, objects with static lifetime, and I think (but
I'm not sure) thrown objects. It's trivial, however, to create
examples where order of destruction is not the reverse of
construction: anytime a temporary is bound to a const reference,
for example, or is used to initialize an other object; or between
static and non-static local variables. E.g.:

void
f()
{
MyClass a ;
static MyClass b ;
}

Order of construction: a, then b. Order of destruction, a, and
sometime well after leaving f, b. (Throw in a call to exit() at
the end of f, and the issue becomes even less clear---a will
never be destructed.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Order of destruction of static members and static objects Victor Bazarov C++ 11 12-09-2009 08:54 AM
virtual base class and construction/destruction order BeautifulMind C++ 7 02-08-2007 12:35 PM
initialization and destruction order for class members Dennis Jones C++ 2 01-05-2007 12:53 AM
Order of destruction of static locals w.r.t. globals Davlet Panech C++ 4 11-10-2006 08:51 PM
Destruction order (e.g. singletons) Stephan Keil C++ 0 11-16-2004 07:15 AM



Advertisments