C ++ learning notes: Calling constructor and destructor order, learning note Constructor
In general, the order of calling the Destructor is the opposite of that of calling the destructor. The constructor is called first, and the corresponding destructor is finally called, at last, the corresponding destructor of the called constructor is called first.
(1) An object defined in a global scope. Its constructor is called before all functions (including the main function) in the file are executed. If a program contains multiple files and global objects are defined in different files, the execution sequence of constructors of these objects is uncertain.
When the main function is executed or the exit function is called, The Destructor is called.
(2) If a partial Automatic Object is defined, its constructor is called when an object is created. If a function is called multiple times, The Destructor is called every time an object is created. The Destructor is called at the end of the function call and when the object is released.
(3) If a static local object is defined in a function, the constructor is called only once when the program calls this function for the first time to create an object, and the object is not released at the end of the call, therefore, you do not call the destructor. The Destructor is called only when the main function ends or the exit function ends.