C ++ learning notes: Calling constructor and destructor order, learning note Constructor

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.