Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Order of de...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 11 of 39 Topic 9813 of 10094
Post > Topic >>

Re: Order of destruct of local variables

by Dan Barbus <dan.barbus@[EMAIL PROTECTED] > Jul 23, 2008 at 02:36 AM

> Don't rely on any order! This is the best choice for you to avoid
> running into undefined behaviour! If you need ordered deletion than
> write a little tool class for that!
>
>      AutoCleanup autoCleanup;
>      AB* ab = autoCleanup(new AB);
>      AC* ac = autoCleanup(new AC);
>
> "ac" will be destroyed before "ab"!

Actually, yes, rely on the order. It is guaranteed, and very useful.
Consider this code (used when working with boost serialization):

     {
         SomeClass *obj = new SomeClass();
>1>     std::ofstream out("file.xml");
>2>     archive::xml_oarchive oarch(out);
         oarch << boost::serialization::make_nvp<SomeClass>("ObjData",
*obj);
         return obj;
     }

Here, you are guaranteed that the 'out' object (1) is valid past the
scope of 'oarch' (2), and still valid in oarch's destructor. Trying to
build around this, (to add extra code to get extra insurance the
destruction order is valid), complicates the code unnecessarily (using
extra accolades, dynamically allocating the memory or using smart
pointers, etc) and makes it uglier and harder to understand.

Regards,
Dan

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 39 Posts in Topic:
Order of destruct of local variables
rkldabs@[EMAIL PROTECTED]  2008-07-21 04:31:10 
Re: Order of destruct of local variables
Greg Herlihy <greghe@[  2008-07-21 14:36:23 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-21 15:16:29 
Re: Order of destruct of local variables
LR <lruss@[EMAIL PROTE  2008-07-21 15:19:25 
Re: Order of destruct of local variables
Thomas Maeder <maeder@  2008-07-21 15:19:12 
Re: Order of destruct of local variables
Thomas Lehmann <t.lehm  2008-07-21 15:17:43 
Re: Order of destruct of local variables
ThosRTanner <ttanner2@  2008-07-21 15:20:25 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-21 23:15:15 
Re: Order of destruct of local variables
Thomas Lehmann <t.lehm  2008-07-22 15:31:22 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-22 15:58:15 
Re: Order of destruct of local variables
Dan Barbus <dan.barbus  2008-07-23 02:36:39 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-23 19:36:23 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-23 23:50:27 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-24 05:15:34 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-24 16:10:28 
Re: Order of destruct of local variables
Bart van Ingen Schenau &l  2008-07-24 16:08:12 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-24 17:22:17 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-24 19:58:15 
Re: Order of destruct of local variables
Eugene Gershnik <gersh  2008-07-25 02:46:32 
Re: Order of destruct of local variables
Nominal Pro <majorscio  2008-07-25 02:43:42 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-25 12:04:42 
Re: Order of destruct of local variables
Francis Glassborow <fr  2008-07-25 12:06:43 
Re: Order of destruct of local variables
Alberto Ganesh Barbati &l  2008-07-25 12:05:15 
Re: Order of destruct of local variables
Le Chaud Lapin <jaibud  2008-07-25 18:20:01 
Re: Order of destruct of local variables
Bart van Ingen Schenau &l  2008-07-25 18:19:47 
Re: Order of destruct of local variables
Alex <aleskx@[EMAIL PR  2008-07-25 18:25:28 
Re: Order of destruct of local variables
Eugene Gershnik <gersh  2008-07-26 13:27:15 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-27 03:18:50 
Re: Order of destruct of local variables
David Abrahams <dave@[  2008-07-27 03:28:18 
Re: Order of destruct of local variables
brangdon@[EMAIL PROTECTED  2008-07-27 15:35:17 
Re: Order of destruct of local variables
Nominal Pro <majorscio  2008-07-27 15:23:49 
Re: Order of destruct of local variables
bjarne <bjarne@[EMAIL   2008-07-27 15:37:41 
Re: Order of destruct of local variables
Alex <aleskx@[EMAIL PR  2008-07-27 15:36:50 
Re: Order of destruct of local variables
brangdon@[EMAIL PROTECTED  2008-07-28 12:37:25 
Re: Order of destruct of local variables
JoshuaMaurice@[EMAIL PROT  2008-07-28 17:58:18 
Re: Order of destruct of local variables
brangdon@[EMAIL PROTECTED  2008-07-29 15:28:19 
Re: Order of destruct of local variables
Mathias Gaunard <loufo  2008-07-27 15:41:11 
Re: Order of destruct of local variables
Nominal Pro <majorscio  2008-07-27 21:50:34 
Re: Order of destruct of local variables
Eugene Gershnik <gersh  2008-07-28 02:32:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 9:13:20 CDT 2008.