Class object initialization sequence

Source: Internet
Author: User
1) All the virtual base class sub-objects will be initialized in the depth-first order from left to right.

2) directly base class sub-objects are constructed in the order they are declared in the class definition.

3) non-static member sub-objects are constructed in the order they are declared in the class definition body.

4) The constructor is executed.

Class B1 {};
Class V1: Public B1 {};
Class D1: virtual public V1 {};

Class B2 {};
Class B3 {};
Class V2: Public B1, public B2 {};
Class D1: virtual public V2, public B3 {};

Class M1 {};
Class M2 {};

Class X: Public D1, public D2 {
M1 _;
M 2 m2 _;
};

First, construct the virtual base class sub-Object
V1: B1: B1 () V1: V1 ()
V2: B1: B1 () B2: B2 () V2: V2 ()

Second, construct non-virtual base-class sub-objects
D1: D1: D1 ()
D2: B3: B3 () D2: D2 ()

Again, construct all members
M1: M1 () m2: M2 ()

Final Construction of X itself
X: X ()

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.