Why there are member initiators

Asked

Viewed 193 times

1

In C++ we have the member initializers const.

1) Why such an appeal exists ?

2) How he acts under the cloths ?

  • 2

    Isn’t it the same? https://answall.com/q/355923/101

  • Actually in this other question is not questioned why this resource exists.

  • That’s all you ask there. And I just didn’t answer because I gave everything but because it exists.

  • Better define what you meant by "below the planes." Do you want to know how the specification defines the behavior of initializer list? Or what is the code generated by compilers? Etc.

1 answer

4


Why such an appeal exists?

To be able to choose the initialization members beyond standard boot.

Let us use a practical example for the explanation:

#include <string>
using std::string;

struct Data {
    string s;
    int i;

    Data(const string &s, int i) {
        this->s = s;
        this->i = i;
    }
};

In the above code (which is obviously not written in the most efficient way of purpose), when the structure Data is instantiated with the constructor defined by us (i.e., the one who receives a std::string and a int), the following steps will occur in order:

  1. Initialize all members in order (in this case, a std::string and a int) with default initialization (i.e., the data member s will have its default constructor called and the string will initialize as empty, and the i will be without defined value).
  2. Call the builder Data(const string &s, int i).
  3. Assign the value of the parameter s to the data member s and assign the value of the parameter i to the data member i.

The problem with this approach is that s is initialized once and then assigned with another value. It would be more efficient if the data member s was initialized once directly with the parameter value s constructor, without requiring further assignment, correct? That’s exactly what the member startup list is for:

struct Data {
    string s;
    int i;

    Data(const string &s, int i) s(s), i(i) {}
};

Using the initialization list as in the above code, we will have the following steps in order:

  1. Initialize all members in order. On behalf of the constructor display a member startup list s(s), i(i), we will use the values passed to them (the parameters s and i) to initialize the members s and i, instead of doing default startup. That is, the member s calls the copy builder, and the i does direct startup.
  2. Call the builder Data(const string &s, int i). In this case, as the constructor is empty, nothing happens.

Note that fewer steps occurred in the second code compared to the first code.

Another important detail as well: constant members and references are only bootable through the member startup list.

How he acts under plans?

The definition of initialization of members by list is rather extensive, so I leave here the reference to the specification: [class.base.init]. For other explanations, please refer to cppreference in initializer list.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.