Results 1 to 4 of 4

Thread: Destructor Order

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    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

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    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.

  3. #3

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    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

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width