![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5
![]() |
Quote:
For example, with; // define a SomeClass
int main()
{
SomeClass x, y;
}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. |
|
|
|
|
|
|
#12 |
|
Hobbyist
Join Date: Sep 2005
Posts: 263
Rep Power: 4
![]() |
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?
|
|
|
|
|
|
#13 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,253
Rep Power: 5
![]() |
Quote:
class X
{
public:
X();
private:
SomeClass a;
SomeOtherClass b;
};
int main()
{
X x;
}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). |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|