|
-
Nov 2nd, 2004, 08:46 PM
#1
Thread Starter
PowerPoster
Destructor Order
If I have a class like this
PHP Code:
#include <homer.hpp>
#include <bart.hpp>
class Marge
{
Marge();
~Marge();
Bart TheBoy;
Homer TheIdiot;
};
int main()
{
Marge TheWife;
return 0;
}
When the program is terminating and Marge gets destroyed, what is the order in which the destructors get called...
Does it destroy TheBoy, TheIdiot then the instance of Marge, TheWife.
Or
Is the order TheWife, TheIdiot, TheBoy?
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Nov 2nd, 2004, 08:57 PM
#2
I guess that depends in what order you use the delete word on them in the destructor in Marge.
[Edit]
Ohhh maybe I missunderstood, first the child objects, then the objects it self.
-
Nov 2nd, 2004, 11:02 PM
#3
Thread Starter
PowerPoster
So before any of the parents destructor is ran it will destruct all the children objects (class instances, vars, etc)?
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Nov 3rd, 2004, 04:47 AM
#4
The order of destruction is the reverse of construction. Construction works like this:
Code:
#include <iostream>
using namespace std;
class farbase1member1_c
{
public:
farbase1member1_c() {
cout << "farbase1member1_c" << endl;
}
};
class base1member1_c
{
public:
base1member1_c() {
cout << "base1member1_c" << endl;
}
};
class base1member2_c
{
public:
base1member2_c() {
cout << "base1member2_c" << endl;
}
};
class base2member1_c
{
public:
base2member1_c() {
cout << "base2member1_c" << endl;
}
};
class member3_c;
class member1_c
{
public:
member1_c() {
cout << "member1_c" << endl;
}
member1_c(const member3_c &) {
cout << "amember1_c" << endl;
}
};
class member2_c
{
public:
member2_c() {
cout << "member2_c" << endl;
}
};
class member3_c
{
public:
member3_c() {
cout << "member3_c" << endl;
}
};
class farbase1_c
{
farbase1member1_c fb1m1;
public:
farbase1_c() {
cout << "farbase1_c" << endl;
}
};
class base1_c : public farbase1_c
{
base1member1_c b1m1;
base1member2_c b1m2;
public:
base1_c() {
cout << "base1_c" << endl;
}
};
class base2_c
{
base2member1_c b2m1;
public:
base2_c() {
cout << "base2_c" << endl;
}
};
class cls_c : public base1_c, public base2_c
{
member1_c m1;
member2_c m2;
member3_c m3;
public:
cls_c() {
cout << "cls_c" << endl;
}
cls_c(int) : m1(m3) {
cout << "acls_c" << endl;
}
};
int main()
{
cls_c object;
cout << "\nInterlude\n" << endl;
cls_c obj2(0);
}
Output:
farbase1member1_c
farbase1_c
base1member1_c
base1member2_c
base1_c
base2member1_c
base2_c
member1_c
member2_c
member3_c
cls_c
Interlude
farbase1member1_c
farbase1_c
base1member1_c
base1member2_c
base1_c
base2member1_c
base2_c
amember1_c
member2_c
member3_c
acls_c
Note that in the second case, the member3_c object passed to the constructor of member1_c is uninitialized and thus in an undefined state. Constructors are always called in the order the objects are declared inside the class, nothing else is considered.
Destruction is the exact opposite. Read the stats from bottom to top.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|