Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ constructor & destructor order

I am trying a code about base class and member construction and destruction and I am confused about some order of constuctor and destructor, the output of this code is:

Base1 constructor
Member1 constructor
Member2 constructor
Derived1 constructor
Member3 constructor
Member4 constructor
Derived2 constructor
Derived2 destructor
Member4 destructor
Member3 destructor
Derived1 destructor
Member2 destructor
Member1 destructor
Base1 destructor

See the first four line, but I fell the order should be

Base1 constructor
Derived1 constructor
Member1 constructor
Member2 constructor

Anyone can give me some explain?

#include "stdafx.h"
#include <fstream>
using namespace std;
ofstream out("order.out");

#define CLASS(ID) class ID { \
public: \
  ID(int) { out << #ID " constructor\n"; } \
  ~ID() { out << #ID " destructor\n"; } \
};

CLASS(Base1);
CLASS(Member1);
CLASS(Member2);
CLASS(Member3);
CLASS(Member4);

class Derived1 : public Base1 {
  Member1 m1;
  Member2 m2;
public:
  Derived1(int) : m2(1), m1(2), Base1(3) {
    out << "Derived1 constructor\n";
  }
  ~Derived1() {
    out << "Derived1 destructor\n";
  }
};

class Derived2 : public Derived1 {
  Member3 m3;
  Member4 m4;
public:
  Derived2() : m3(1), Derived1(2), m4(3) {
    out << "Derived2 constructor\n";
  }
  ~Derived2() {
    out << "Derived2 destructor\n";
  }
};

int main() {
  Derived2 d2;
} ///:~
like image 661
user1279988 Avatar asked Mar 27 '12 12:03

user1279988


1 Answers

Constructors are called upwards in the hierarchy:

- base class member objects
- base class constructor body
- derived class member objects
- derived class constructor body

The output is correct.

Let's simplify your code:

struct BaseMember
{
   BaseMember() { cout << "base member" <<endl; }
};
struct Base
{
   BaseMember b;
   Base() { cout << "base" << endl; }
};
struct DerivedMember
{
   DerivedMember() { cout << "derived member" << endl; }
};
struct Derived : public Base
{
   DerivedMember d;
   Derived() { cout << "derived" << endl; }
};

Derived d;

When d is created, it will first create the Base part. Before it enters the constructor body, all member objects are initialized. So BaseMember is the first object initialized.

Next, the constructor of Base is entered.

Before, the constructor of Derived enters, member objects of Derived are initialized, so DerivedMember is created, next Derived constructor is called.

This happens because when you enter the constructor body of a derived class, base classes and member objects must be completely initialized.

EDIT As Matthieu pointed out, the order in which the member objects are initialized is specified by the order in which they appear in the class definition, not the order in which they appear in the initializer list.

like image 59
Luchian Grigore Avatar answered Sep 24 '22 11:09

Luchian Grigore