List of C + + initializer functions

Source: Internet
Author: User

The following three scenarios require the use of the Initialize member list:

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

Second, the class member that initializes the const modifier is required;

Third, you need to initialize the reference member data;

Reason:

C + + can define member variables of reference types, and member variables of reference types must be initialized in the constructor's initialization list. For cases where a class member is a const or reference type, the assignment operation is not allowed (obviously, const is prevented from being incorrectly assigned, the reference type must be assigned together), so it can only be initialized with the initialization list alignment. Member types are classes that do not have a default constructor. If no display initialization is provided, the compiler implicitly uses the default constructor of the member type, and if the class does not have a default constructor, the compiler will fail with the default constructor. (That is, these three cases must use the initialization list)

We define a person class as follows:

Class Person {

Public

Person () {}//default constructor function

Person (string name, string phone, string addr)

{

M_name = name; To initialize a data member with an assignment

M_phone = phone;

m_addr = addr;

}

Private

const string M_name;

const string M_phone;

const string M_ADDR;

};

After compiling, it is wrong to find the second constructor with parameters for this class. We create a Person object:

Person P ("Marcky", "13233232", "Cqupt"); Calling the constructor with parameters to create a person object creates an object in two steps:

One, allocating the actual space from memory to the object P, whose three string object's data member is called by the default constructor is initialized to null. So far, 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 constructor list:

Person (string name, string phone, string addr)

: M_name (name), M_phone (phone), m_addr (addr) {}//colon Start define initialization list the constructor for creating an object using the initialization list is also done in the two steps above. The difference is that when you create a data member of an object, you are not using the default constructor, but instead call the appropriate constructor with the specified parameters to create a specific object instead of an empty object. As a result, the specific value of the object's data member is assigned to the corresponding member when the object is created, instead of modifying the data member through the assignment statement after the object is created, so the constructor initializes the list to create the object with the const data member successfully.

A class-type member without a default constructor, if it is not initialized in the initialization list, when the object is created, the compiler will call the default constructor to create the object because it does not specify the corresponding argument, and it 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 order in which the members are defined.

NOTE 2 : Using an initialization list is more efficient if assigning a value in a constructor is a copy, and if it is initialized in the initialization list, the assignment and initialization are of course not as efficient.

List of C + + initializer functions

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.