PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Order of destruction
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Order of destruction

Réponse
 
LinkBack Outils de la discussion
Vieux 03/04/2008, 15h43   #1
Lilith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Order of destruction

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
  Réponse avec citation
Vieux 03/04/2008, 16h35   #2
Martin York
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Order of destruction

On Apr 3, 7:43 am, Lilith <lil...@dcccd.edu> 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.
  Réponse avec citation
Vieux 03/04/2008, 17h50   #3
Lilith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Order of destruction

On Thu, 3 Apr 2008 08:35:52 -0700 (PDT), Martin York
<Martin.YorkAmazon@gmail.com> wrote:

>On Apr 3, 7:43 am, Lilith <lil...@dcccd.edu> 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
  Réponse avec citation
Vieux 04/04/2008, 00h22   #4
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Order of destruction

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.
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.

  Réponse avec citation
Vieux 04/04/2008, 08h40   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Order of destruction

On Apr 4, 1:22 am, Ron Natalie <r...@spamcop.net> 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:james.kanze@gmail.com
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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h43.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13367 seconds with 13 queries