List of constructor Initialization

Source: Internet
Author: User

First, what is the output of the running C ++ code?

class A{private:int n1;int n2;public:A(): n2(0) , n1(n2 + 2){}void Print(){cout<<"n1:"<<n1<<",n2:"<<n2<<endl;}};int main(void){A a;a.Print();return 0;}

Answer: The output N1 is a random number, and N2 is 0. In C ++, the initialization sequence of member variables is the same as that of the variables declared in the type, but it is irrelevant to the sequence of member variables in the initialization list of the constructor. Therefore, in this question, N1 is initialized first, while N2, the initial N1 parameter, is a random value. Therefore, N1 is a random value. When N2 is initialized, It is initialized according to the parameter 0, so n2 = 0.
The initialization list of the constructor only specifies the values used to initialize members, and does not specify the order of initialization execution. The order in which members are initialized is to define the order of members. The first member is initialized first, then the second one, and so on. That is to say, the C ++ compiler can easily obtain the parameter list of the constructor, obtain the parameter, and initialize the member variable according to the declared sequence of the member variable (this is because, the declared variables may depend on the declared member variables. Therefore, you must declare them first, and then declare them before initialization ).
As you may see, It is very troublesome. When a data member in the class is initialized based on other data members, the initialization list order cannot be different from the Declaration Order of the member variables. Otherwise, an unexpected error occurs.
This is indeed the case, so some people will think that I simply put all the operations in the initialization list in the constructor's function body to assign values to data members, this can be done, but sometimes the initialization list of the constructor is required.
Some data members must be initialized in the constructor initialization list. For such members, assigning values to them in the constructor does not work.No member of the default constructor class type, member variables of the const type, and member variables of the reference type must be initialized in the constructor initialization list..
For example, the following constructor definition is incorrect:

class A{private:int i;const int j;int &k;public:A(int ii){i = ii;j = ii;k = ii;}};

Remember, you can initialize const objects or reference objects, but you cannot assign values to them. Before starting to execute the constructor's function body, you must complete initialization. the only opportunity to initialize the const or referenced data member is in the constructor's initialization list.
For example, the following constructor definition is correct:

class A{private:int i;const int j;int &k;public:A(int ii) : i(ii) , j(i) , k(ii){}A() : j(0) , k(i){ }};int main(void){A a;return 0;}

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.