C + + initialization order correlation

Source: Internet
Author: User

C + + initialization order correlation

1. Set the order of function default parameters: right-to-left

Cause: The order in which the parameters are filled in is left to right when the function is called, so the default parameters must be given from right to left

2. class data member initialization list is initialized in the order: right to left

Example:

Class Classa{public:    ClassA (): m_n2 (0), m_n1 (m_n2+2)    {}    void Print ()    {        cout<< "m_n1:" < < M_n1 << "m_n2:" << m_n2 <<endl;    }    ~classa ()    {}private:    int m_n1;    int m_n2;}; int _tmain (int argc, _tchar* argv[]) {    ClassA obj;    Obj. Print ();    return 0;}

Output Result: M_n1:0xccccccce m_n2:0 (m_n2 initial value is 0XCCCCCCCC)

3. Initialization order of data members and constructors for a class: initializes the data member of the class before calling the constructor

Cause: When you call the constructor, the constructor may need to access the data members, so the data members need to be initialized well in advance.

4. The order in which the data members of the class and the destructor are destroyed: first call the destructor, then destroy the data members of the class

Cause: When you call a destructor, the destructor may need to access the data members, so the data members need to be refactored later.

5. Constructor call order (according to some elder sister's argument: first grandfather, then father, and son):

A. Base class constructor B. Child object constructor c. Derived class constructor

6. Sequence of calls for destructors:

A. Destructors for derived classes B. Destructors for sub-objects C. Destructors for base classes

Not finished

C + + initialization order correlation

Related Article

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.