C ++ core principles C.48: If the constructor needs to initialize member by a constant, the use of more appropriate class initializers

C.48: Prefer in-class initializers to member initializers in constructors for constant initializers

C.48: If the constructor needs to initialize member by a constant, the use of more appropriate class initializers

 

 

 

Reason (reason)

 

 

 

Makes it explicit that the same value is expected to be used in all constructors. Avoids repetition. Avoids maintenance problems. It leads to the shortest and most efficient code.

Clearly expressed the hope that all constructors use the same value. Avoid maintenance issues. You can generate the shortest, most efficient code.

 

 

 

 

Example, bad (negative sample)

 

 

 

 

class X {   // BAD
    int i;
    string s;
    int j;
public:
    X() :i{666}, s{"qqq"} { }   // j is uninitialized
    X(int ii) :i{ii} {}         // s is "" and j is uninitialized
    // ...
};

 

How would a maintainer know whether j was deliberately uninitialized (probably a poor idea anyway) and whether it was intentional to give s the default value ""  in one case and qqq in another (almost certainly a bug)? The problem with j (forgetting to initialize a member) often happens when a new member is added to an existing class.

Maintenance personnel how can I know whether j is deliberately not initialized (although this may be a bad idea) it? S will know how a situation is initialized to "" while another case is initialized to "qqq" is intentional it (This is almost a mistake)? On the issue of j (forgot to initialize a member) often occurs in adding new members to the class time.

 

 

 

 

Example (Example)

 

 

 

 

class X2 {
    int i {666};
    string s {"qqq"};
    int j {0};
public:
    X2() = default;        // all members are initialized to their defaults
    X2(int ii) :i{ii} {}   // s and j initialized to their defaults
    // ...
};

 

 

 

 

Alternative (alternative)

 

 

 

We can get part of the benefits from default arguments to constructors, and that is not uncommon in older code. However, that is less explicit, causes more arguments to be passed, and is repetitive when there is more than one constructor:

By using the default constructor arguments, we can get some benefits. This situation can be very common in the old code. However, this approach lacks clarity, it causes more parameters are passed, and causes the code is repeated in the presence of more than one constructor, a lot of trouble.

 

class X3 {   // BAD: inexplicit, argument passing overhead
    int i;
    string s;
    int j;
public:
    X3(int ii = 666, const string& ss = "qqq", int jj = 0)
        :i{ii}, s{ss}, j{jj} { }   // all members are initialized to their defaults
    // ...
};

 

 

 

 

Enforcement (Suggestions)

 

 

 

  • (Simple) Every constructor should initialize every member variable (either explicitly, via a delegating ctor call or via default construction).

    (Simple) all constructors should initialize each member (can be explicitly entrusted by the constructor or default constructor)

  • (Simple) Default arguments to constructors suggest an in-class initializer may be more appropriate.

  • (Simple) for the default constructor parameters using a class initializer may be more appropriate choice.


 

I think this article helpful? Welcome thumbs up and share it with more people.

Read more updated articles, please pay attention to micro-channel public number of object-oriented thinking []

Published 408 original articles · won praise 653 · views 290 000 +

Guess you like

Origin blog.csdn.net/craftsman1970/article/details/104541262