C++ initialization function list in detail

  • 2020-04-02 01:37:54
  • OfStack

The initialization member list is needed in three situations:

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

Two, need to initialize the const-decorated class members;

Three, need to initialize the reference member data;

The reason:
C++ can define member variables for reference types that must be initialized in the constructor's initialization list. For class members to be const modifiers, or for reference types, no assignment is allowed (const is obviously to prevent misassignment, and reference types must be defined to be assigned together), so you can only initialize with the initialization list alignment. A member type is a class that has no default constructor. If the 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 fails to attempt to use the default constructor. (that is, the initialization list must be used in all three cases)

We define a Person class as follows:


class Person {
public:
  Person() { } //default constructor function
  Person(string name, string phone, string addr)
  {
    m_name = name; //You want 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;
};

When compiled, the second constructor with an argument for this class was found to be incorrect. We create a Person object:

Person p (" marcky ", "13233232", "cqupt"); // calling the constructor with parameters to create a Person object the process of creating an object is divided into two steps:

A, Allocates the actual space from memory to the object p, whose data members of the three string objects are initialized to null by the default constructor of the call. So far, all three data members of object p are empty strings.

Second, Execute the body statement of the called constructor, complete the assignment to the data member, and achieve the desired creation of a specified Person object instead of an empty object.

As you can see from the second step above, we are performing an assignment on 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 the constructor to initialize the list:

Person(string name, string phone, string addr)
: m_name (name), m_phone (phone), m_addr (addr) {} / / colon start defining initialization list using initialization list constructor for creating an object is also done through the above two steps, the difference is to create the object is not the default constructor is used when the data members, but the corresponding constructor calls according to the specified parameters, in order to create a particular object, rather than an empty object. In this way, the specific value of the data member of the object 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 pair object with const data member can be successfully created by initializing the list with the constructor.

There is no class type member with a default constructor, and if it is not initialized in the initialization list, the compiler will call the default constructor to create the object without specifying the corresponding "arguments", which is bound to fail.

Note 1: The order in which data members are initialized is independent of the order in which the constructor initializes the list, but is in accordance with the order in which the members are defined.

Note 2: It's more efficient to use an initialization list, which is a copy if you're in the constructor, and an initialization if you're in the initialization list, which is of course not as efficient as an initialization.


Related articles: