The call order of constructor and destructor in C++

  • 2020-05-12 02:58:59
  • OfStack

preface

When using constructors and destructors, you need to pay special attention to the timing and order in which they are called. In the case of 1, the order in which the destructor is called is exactly the opposite of the order in which the constructor is called: the constructor that is called first is the one whose corresponding destructor (in the same object) is called last, and the constructor that is called last is the one whose corresponding destructor is called first.

In simple terms, the order of its constructor is 1 sentence:

Base class constructor - > Member's constructor - > Statement in constructor body

Take a look at the following code example:


#include <iostream>

using namespace std;

class A {
 public:
  A() { cout << "A()" << endl; }
  ~A() { cout << "~A()" << endl; }
};

class B {
 public:
  B(int b) { cout << "B(" << b << ")" << endl; }
  ~B() { cout << "~B()" << endl; }
};

class C {
 public:
  C(int c) { cout << "C(" << c << ")" << endl; }
  ~C() { cout << "~C()" << endl; }
};

class D {
 public:
  D() { cout << "D()" << endl; }
  ~D() { cout << "~D()" << endl; }
};

class E: public B, public A {
 public:
  D d;
  C c;
  E(): c(3), B(5) { cout << "E()" << endl; }
  ~E() { cout << "~E()" << endl; }
};

int main()
{
 E e;

 return 0;
}

Can you see the output of this code?

As mentioned above, the first thing to execute is the constructor of the base class. However, the C++ language supports multiple inheritance, so what is the order in which a class is constructed when it inherits multiple classes? The answer is: simply in the order of inheritance.

Next, the constructor of its member variables is executed in the same simple order as the member variables in the class code.

Finally, the concrete code in the constructor is executed.

Note that in the above procedure, the order of execution is independent of the order of the constructor parameters.

Creating an object via new calls its constructor, and deleting an object via delete calls its destructor.

If an object is not deleted using delete, the destructor is executed in the opposite order as the constructor.

So, the output of the above code is:


B(5)
A()
D()
C(3)
E()
~E()
~C()
~D()
~A()
~B()

Here's induction 1 for when to call the constructor and destructor:

1) for an object defined in the global scope (that is, an object defined outside of all functions), its constructor is called before all functions in the file (including main functions) are executed. However, if there are multiple files in a program and global objects are defined in different files, the execution order of the constructors of these objects is uncertain. When the main function is completed or the exit function is called (at which point the program terminates), the destructor is called.

2) if a local automatic object is defined (for example, an object is defined in a function), its constructor is called when the object is created. If the function is called multiple times, the constructor is called each time the object is created. The destructor is called when the function call ends and the object is released.

3) if the static (static) local object is defined in the function, the constructor will be called only once when the program calls this function to establish the object, and the object will not be released at the end of the call, so the destructor will not be called, and only when the main function ends or the exit function ends the program will the destructor be called.

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: