C ++ object-oriented Review (iii) -- Call Sequence of constructor and destructor

Source: Internet
Author: User

Sequence of calling constructor and destructor

The first constructor and the second constructor.

When to call constructor and destructor

(1) An object defined in a global scope. Its constructor is executed before all functions in the file are executed. However, whenProgramWhen multiple files are defined and global variables are defined in different files, the call sequence of the object constructor is uncertain. When the main function ends or the exit function is run, call destructor

(2) when defining a partial automatic object, the object is created to call its constructor. If a function is called multiple times, you must call the constructor after each object creation. At the end of the function call, you can call the destructor.

(3) When a static object is defined as static, the constructor is called only once when the object is created. When the main function is completed or exit, the Destructor is called.

 

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.