C + + initialization function List Detailed resolution of _c language

Source: Internet
Author: User
Tags modifier

You need to use the initialization member list in the following three situations:

First, the data member that needs to initialize is the case of the object;

Second, class members that need to initialize the const modifier;

Third, the reference member data needs to be initialized;

Reason:
C + + can define a member variable of a reference type, and a member variable of a reference type must be initialized in the constructor's initialization list. For a class member to be a const modifier, or a reference type, it is not allowed to assign operations, (obviously, the const is to prevent the wrong assignment, the reference type must define the assignment), and therefore can only be initialized with the initialization list alignment. A member type is a class that does not have a default constructor. If a display initializer is not provided, the compiler implicitly uses the default constructor for the member type, and if the class does not have a default constructor, the compiler will fail to attempt to use the default constructor. (That is, these three cases must use the initialization list)

We define a person class as follows:

Copy Code code as follows:

Class Person {

Public

Person () {}//default constructor function

Person (string name, string phone, string addr)

{

M_name = name; You want to initialize data members by assigning values

M_phone = phone;

m_addr = addr;

}

Private

const string M_name;

const string M_phone;

const string M_ADDR;

};


It was found that the constructor of the second parameter of this class was incorrectly compiled. We create a Person object:

Person P ("Marcky", "13233232", "Cqupt"); Calling a constructor with a parameter creating a person object creates an object in two steps:

First, allocate the actual space from memory to the object P, the data member of its three string objects is the default constructor called is initialized to null. That said, at this point, the three data members of the object p are an empty string.

The function-body statement of the constructor that executes the call completes the assignment to the data member to achieve what we expect to create a specified person object instead of an empty object.

As you can see from the second step above, we are assigning operations to three const objects, which is obviously not allowed, so creating a person with this constructor will fail. To successfully create a specific person object, we need to initialize the list with the constructor function:

Person (string name, string phone, string addr)
: M_name (name), M_phone (phone), m_addr (addr) {}//colon beginning to define initialization list using the initialization list to create the constructor of an object is also done through the two steps above. The difference is that when you create a data member of an object, instead of the default constructor, you invoke the corresponding constructor based on the specified arguments to create a specific object instead of an empty object. As a result, the specific values of the data members of the object are assigned to the corresponding members when the object is created, rather than modifying the data member by an assignment statement after the object is created, so that using the constructor initialization list can successfully create the object with the const data member.

A member of a class type that does not have a default constructor, if it is not initialized in the initialization list, then when the object is created, the compiler calls the default constructor to create the object without specifying the corresponding "argument", which will inevitably end in failure.

Note 1: the order in which data members are initialized is independent of the order in the constructor initialization list, but is consistent with the definition order of the members.

NOTE 2: using an initialization list is more efficient, and if you assign a value to a constructor, it is a copy, and if it is initialized in the initialization list, the assignment and initialization are certainly not as efficient.

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.