Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 13th, 2006, 3:25 AM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by Cache
Maybe not in C#. In C++ objects are destructed in reverse order to there construction. FILO, if I remeber correctly.
This is true in some cases but not in others.

For example, with;
//  define a SomeClass

int main()
{
    SomeClass x, y;
}
the order of construction of x and y is unspecified, as is the order of their eventual destruction as main() returns. Any code that relies on x being created first (or destroyed first) is asking for trouble.

When using dynamic creation/destruction of objects (i.e. operator new and delete), there is no guarantee that an object created first is destroyed first (the order of creation and destruction is under programmer control).

If static objects are declared in two different functions (or in two separate source files at file scope), there is no relationship between their order of construction, or the order of their destruction.
grumpy is offline   Reply With Quote
Old May 13th, 2006, 3:55 AM   #12
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Interesting, I actually thought the order of construction/destruction is guaranteed to be as I stated above, with the exception of dynamically allocated objects. Does this also mean that embedded objects of a class are not guaranteed to be destructed in reverse order to their construction?
Cache is offline   Reply With Quote
Old May 13th, 2006, 5:40 AM   #13
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by Cache
Interesting, I actually thought the order of construction/destruction is guaranteed to be as I stated above, with the exception of dynamically allocated objects. Does this also mean that embedded objects of a class are not guaranteed to be destructed in reverse order to their construction?
I assume by "embedded object" that you mean members of a class. For example;
class X
{
    public:
        X();
    private:
      SomeClass a;
      SomeOtherClass b;
};

int main()
{
    X x;
}
In this example, there is no guaranteed order for creating x.a and x.b. x.a and x.b will be destroyed in reverse order of their construction, but as the order of construction is unspecified......

What is guaranteed, in this example, is that X's constructor is invoked after the members a and b are constructed. And X's destructor is invoked before a and b are destructed.

With class inheritence, the standard is very specific about order of construction and destruction (base classes are constructed in a specific order before the constructor of the derived class is invoked). And, the order of destruction is the reverse of construction (i.e. derived class destructor invoked first, then the base class destructors in reverse order from their order of construction).
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:53 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC