Constructor and copy function call order


Constructor and copy function call order

Class A;
Class B;
void F() 
{
  A a;
  B b;
}//我们定义了a,b两个类,那么a,b构造函数的调用顺序和析构函数的顺序是怎样的呢?

The construction order is constructed according to the order of the statements, so the order of the constructor calls is to call a first, then call b.

Destruction is an operation performed at the end of a program, in the reverse order of construction, hence b, a.

The overall order is ab construction, then ba destruction.

Let's just look at the destructuring order

C c;
int main()
{
A a;
B b;
static D d;
  
return 0;
}//其中A,B,C,D都是类

①In addition to knowing that the order of destructors is reversed from the order of constructor calls,

②Here we must know that static changes the scope of the object's existence, and we need to wait for the end of the program to destruct and release the object.

③ And c is a global variable, so it is constructed before a, b, d, so c is the last destructor.

According to the above three points, the order of destruction is b, a, d, c

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324303911&siteId=291194637