constructor Person
constructor Student
constructor PhDStudent
destructor Student
destructor Person
destructor Student
destructor Person
destructor Student
destructor Person
destructor Student
destructor Person
destructor PhDstudent
destructor Student
destructor Person
Now I understand that line PhDStudent james; make constructors in order that it makes and call last 3 destructors in reverse order,
but I don't understand how line returnPhDStudent(returnStudent(returnPerson(james))); call all this destructors.
Can someone help me?
Edit:
Also I run this code on this site and this is output that I get
1 2 3 4 5 6 7 8 9 10 11 12
constructor Person
constructor Student
constructor PhDStudent
destructor Student
destructor Person
destructor Student
destructor Person
destructor Student
destructor Person
destructor PhDstudent
destructor Student
destructor Person
Lines 1,2,3 of the output are the result of line 31. No surprise.
Lines 12,13,14 are the result of james going out of scope when the program exits.
That leaves lines 4-11 in which we see the student and person destructor called 4 times.
Line 12: You're passing and returning student by reference. Therefore no constructor or destructor is called.
Lines 20,28: You're passing and returning student by value. Therefore the compiler makes a copies of student to push on the stack. Since you don't provide a copy constructor, the compile provides one (without a cout statement). When the copy of student is popped off the stack, the standard destructor is called.
Let's take apart line 32. The first thing to execute is returnPerson(james).
james is passed and returned by reference leaving a reference on the stack. No constructors or destructors called.
Next, returnStudent() is called. The compile now calls student's copy constructor to convert the reference left on the stack into an instance that is passed by value. A copy of student is then returned by value and left on the stack. When this value is popped off the stack, student and persons destructors get called.
Finally returnPhDStudent() is called. The above process repeats since student is again passed by value;.
And the difference in output is presumably because the first output was from a version of the program that didn't use references on one of the functions.
Person
Student
PhDStudent
Person
Student (copy)
Person
Student (copy)
~Student
~Person
Person
Student (copy)
~Student
~Person
Person
Student (copy)
~Student
~Person
~Student
~Person
~PhDstudent
~Student
~Person
Is this normal thing that different compilers get different results? Or should I try to write program in the way that prevent this to happen? Is there literature that I can use that teach how to do that?
Edit:
I now saw that output is same but in little bit different order.