Constructor / Destructor order
I was wondering if the order destructors are called is always the opposite of the order the constructors are called..
In other words
if I had types TypeA, TypeB, and TypeC and said
TypeA A;
TypeB B;
TypeC C;
would the destructors always be called in C, B, A order?
is this a standard or just something that my compiler just seems to do (I''ve tested it and it seems to do this every time - but I just wanna be sure)
Maybe I should just RTFM .. I know..
Yup your absolutly right in C++ destructors are called in the reverse order that the constructers were called every time. Its a standard.
Edited by - UraniumRod on 2/9/00 2:10:20 PM
Edited by - UraniumRod on 2/9/00 2:10:20 PM
------------------------------"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.
From what I understand about the standard (been a while since I have looked it over, so take this with a grain of salt) this is true when objects are created on the stack or contained within a class. However I don''t think this behavior is defined for objects that are in global scope.
For example:
function scope with objects created on the stack
foo(void)
{
Type A;
Type B;
Type C;
}
constructors are called in this order Type::A(), Type::B(), Type::C()
destructors are called in this order Type::C(), Type::B(), Type::A()
Class scope with objects created inside a class
class foo
{
public:
Type A;
Type B;
Type C;
};
constructors are called in this order Type::A(), Type::B(), Type::C()
destructors are called in this order Type::C(), Type::B(), Type::A()
Global scope with objects created on the heap
Type A;
Type B;
Type C;
foo(void)
{
}
constructors are called in an order that is compiler dependant.
destructors are called in the opposite order the contructors were called.
I do think that most compilers will destroy global objects in the order they created them, but I don''t think they will be created in any paticular order that you can depend on.
I hope this helps.
For example:
function scope with objects created on the stack
foo(void)
{
Type A;
Type B;
Type C;
}
constructors are called in this order Type::A(), Type::B(), Type::C()
destructors are called in this order Type::C(), Type::B(), Type::A()
Class scope with objects created inside a class
class foo
{
public:
Type A;
Type B;
Type C;
};
constructors are called in this order Type::A(), Type::B(), Type::C()
destructors are called in this order Type::C(), Type::B(), Type::A()
Global scope with objects created on the heap
Type A;
Type B;
Type C;
foo(void)
{
}
constructors are called in an order that is compiler dependant.
destructors are called in the opposite order the contructors were called.
I do think that most compilers will destroy global objects in the order they created them, but I don''t think they will be created in any paticular order that you can depend on.
I hope this helps.
I''ll second that, destructors are called in the opposite order that the constructors were called in.
Thus, to clarify
ObA A_Instance;
ObB B_instance;
ObC C_Instance;
// given those statements, the destructors would be called
in this order:
C_Instance''s destructor
B_Instance''s destructor
A_Instance''s destructor
Definately straight.
Take it easy,
-Mezz
Thus, to clarify
ObA A_Instance;
ObB B_instance;
ObC C_Instance;
// given those statements, the destructors would be called
in this order:
C_Instance''s destructor
B_Instance''s destructor
A_Instance''s destructor
Definately straight.
Take it easy,
-Mezz
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement