C ++ constructor Call Sequence

Source: Internet
Author: User
Tags c constructor

Summary:

First, the constructor is called in the inheritance order. If there is a parameter, the parameter is passed. If there is no parameter, the default constructor is called.

Then, the constructor is called according to the sequence defined by data members (data members of the class type ).

 

# Include <iostream>

Using STD: cout;
Using STD: Endl;

Class B1
{
Public:
B1 (int I)
{Cout <"consb1" <I <Endl ;}
};
Class B2
{
Public:
B2 (Int J)
{Cout <"consb2" <j <Endl ;}
};
Class B3
{
Public:
B3 ()
{Cout <"consb3 *" <Endl ;}
};
Class C: Public B2, public B1, public B3
{
Public:
C (int A, int B, int C, int D, int e)
: B1 (A), memberb2 (D), memberb1 (C), B2 (B)
{M = E; cout <"consc" <Endl ;}
PRIVATE:
B1 memberb1;
B2 memberb2;
B3 memberb3;
Int m;
};
Int main ()
{
C OBJ (1, 2, 3, 4, 5 );
Return 0;
}

________________________________________
Running result: consb2 2
Consb1 1
Consb3 *
Consb1 3
Consb2 4
Consb3 *
Consc

(I did not understand it at all. I 'd like to explain in detail the calling sequence and reasons of these constructor functions. I am very grateful for the fact that B3 * appears twice !)

 

--------------------------------

// Follow the inheritance sequence: B2, B1, B3

// Step 1: Inherit B2 first, find B2 (B) in the initialization list, and print "constb22"

// Step 2: Inherit B1, find B1 (A) in the initialization list, and print "constb11"

// Step 3: Inherit B3 again. If B3 (x) cannot be found in the initialization list, call the default constructor B3 () in B3 and print "constb3 *"

// Define the sequence of data members: memberb1, memberb2, and memberb3.

// Step 4: Find memberb1 (c) in the initialization list and initialize a B1 object. If C is used as the parameter, call the B1 constructor and print "constb13"

// Step 5: Find memberb2 (d) in the initialization list and initialize a B2 object. If D is used as the parameter, call the B2 constructor and print "constb24"

// Step 6: If memberb3 (X) is not found in the initialization list, call the default constructor B3 () in B3 and print "constb3 *"

// The remaining part of the object initialization is completed, that is, the function body of the C constructor: {M = E; cout <"consc" <Endl ;}

// Step 7: Print "consc"

Back to your main question: why does B3 * appear twice?

The first time it was because it inherited B3, although you did not see B3 (x) or B3 () in the initialization list of the c constructor (), but it does not mean that the B3 constructor is not playing a role. In fact, B3 is implicitly initialized. It doesn't matter if you do not write B3 () because the B3 constructor has no parameters. This is omitted here. B1 and B2 are both Explicit initialization because they all require parameters. The second time is because C has the data member memberb3. Once again, you did not see the memberb3 () You want to appear in the initialization list of the constructor C. Obviously, this is another implicit initialization. The B3 constructor is called secretly again. Every time the B3 constructor is called, "consb3 *" is printed *". Two calls, and two "consb3 *" are printed naturally *".

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.