Constructor/Destructor

I'm referencing pages 209-14 of C++ for dummies, 6th edition. It's a minor point, but I'm confused none the less. It's saying that objects that contain other objects as members construct the member objects before the parent object. I get that, except there are two programs.

The first, this one...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
//  ConstructMembers - the member objects of a class
//                     are each constructed before the
//                     container class constructor gets
//                     a shot at it
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Course
{
  public:
    Course()
    {
        cout << "constructing course" << endl;
    }
};

class Student
{
  public:
    Student()
    {
        cout << "constructing student" << endl;
        semesterHours = 0;
        gpa = 0.0;
    }
  protected:
    int  semesterHours;
    float gpa;
};
class Teacher
{
  public:
    Teacher()
    {
        cout << "constructing teacher" << endl;
    }
  protected:
    Course c;
};
class TutorPair
{
  public:
    TutorPair()
    {
        cout << "constructing tutorpair" << endl;
        noMeetings = 0;
    }
  protected:
    Student student;
    Teacher teacher;
    int   noMeetings;
};

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "Creating TutorPair object" << endl;
    TutorPair tp;

    // wait until user is ready before terminating program
    // to allow the user to see the program results
    system("PAUSE");
    return 0;
}


...produces THIS output:

Creating TutorPair oject
constructing student
constructing course
constructing teacher
constructing tutorpair
Press any key to continue . . .


But the SECOND program, this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
//  DestructMembers - this program both constructs and
//                    destructs a set of data members
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

class Course
{
  public:
    Course()  { cout << "constructing course" << endl; }
    ~Course() { cout << "destructing course" << endl;  }
};

class Student
{
  public:
    Student() { cout << "constructing student" << endl;}
    ~Student() { cout << "destructing student" << endl; }
  protected:
    int  semesterHours;
    float gpa;
};
class Teacher
{
  public:
    Teacher()
    {
        cout << "constructing teacher" << endl;
        pC = new Course;
    }
    ~Teacher()
    {
        cout << "destructing teacher" << endl;
        delete pC;
    }
  protected:
    Course* pC;
};
class TutorPair
{
  public:
    TutorPair(){cout << "constructing tutorpair" << endl;}
   ~TutorPair(){cout << "destructing tutorpair" << endl; }
  protected:
    Student student;
    Teacher teacher;
};

TutorPair* fn()
{
    cout << "Creating TutorPair object in function fn()"
         << endl;
    TutorPair tp;

    cout << "Allocating TutorPair off the heap" << endl;
    TutorPair*  pTP = new TutorPair;

    cout << "Returning from fn()" << endl;
    return pTP;
}


int main(int nNumberofArgs, char* pszArgs[])
{
    // call function fn() and then return the
    // TutorPair object returned to the heap
    TutorPair* pTPReturned = fn();
    cout << "Return heap object to the heap" << endl;
    delete pTPReturned;

    // wait until user is ready before terminating program
    // to allow the user to see the program results
    system("PAUSE");
    return 0;
}


Produces THIS output:

Creating TutorPair oject in function fn()
constructing student
constructing teacher
constructing course
constructing tutorpair
Allocating TutorPair off the heap
constructing student
constructing teacher
constructing course
constructing tutorpair
Returning from fn()
destructing tutorpair
destructing teacher
destructing course
destructing student
Return object to the heap
destructing tutorpair
destructing teacher
destructing course
destructing student
Press any key to continue . . .


What's going on there? The destruction are going in reverse order in the second program like it says it should, but unlike in the first program, course and teacher are reversed in the order they're constructed, even though course is a member of teacher. Why is that? Does it have something to do with course being a pointer in the second program?
YEs: In destructor for teacher first routine is an output "destructing teacher", second is delete, which will call destructor for course
But that's the case in the first program as well.
No, members are default constructed before actual class constructor is called. In second program there is an pointer, which we manually create/delete, so it will be callde after every other member in class.
1
2
        cout << "constructing teacher" << endl;
        pC = new Course;

If you swap these two lines, you will get another order.
Last edited on
No, the order of construction is not the same in the two programs.

In the first program, course is a member (line 42). It is constructed as part of the initialization of Teacher (but prior to executing teacher's constructor).

In the second program, course is a pointer (line 40). During teacher's initialization (before teacher's constructor is called), no constructor of course is called. When teacher's constutor executes, it then allocates course.
Oh, I think I get it! so basically, you're only declaring a pointer in the second program, so it doesn't actually call the course constructor, is that right?
Correct. The pointer remains uninitialized (doesn't point to anything) until you assign it a value by calling new Course, which allocates memory for Course and calls its constructor.
Topic archived. No new replies allowed.