Invocation times and invocation order of constructors, copy constructors, and destructors
Objects are constructed from the bottom up , when an object is created, the constructor of the base class is called first, and then the constructor of the next derived class is called, and so on until the constructor of the most derived class that derives the most number of derived classes is reached. Because, when the constructor is initially constructed, it is always called the constructor of its base class before it starts executing its constructor body, and when the direct base class constructor is called, the default constructor of the direct base class is called if no specific description is 0. When an object is refactored, its order is reversed . The following is a brief introduction to these three functions.
constructor function
1. Constructors cannot have return values
2. The default constructor will automatically invoke the default constructor to initialize the object, and the default constructor will have all data members
Initialize to zero or null
3. When you create an object, the system automatically calls the constructor
Destructors
Destructors have no parameters and no return values. cannot be overloaded, which means that only one destructor can be defined in a class
If a destructor is not defined in a class, the system automatically generates a default destructor, which is an empty function and does nothing
Calling condition: 1. The object defined in the function body, when the function execution ends, the destructor of the object's class is automatically called;
2. An object that is dynamically constructed with the new operator when it is released using the delete operator.
Copy constructor
A copy constructor is actually a constructor, with all the attributes of a generic constructor, with the same name as the owning class name. There is only one parameter in the copy constructor, which is a reference to a similar object.
!!! is called in three cases:
1. When a known object of the class is used to initialize another object of the class;
2. The formal parameter of the function is the object of the class, when the function is combined with the parameter and the argument;
3. The return value of the function is the object of the class, and the function returns the caller after execution.
Invocation times and invocation order of constructors, copy constructors, and destructors