Why should I prefer to use member initialization lists?

后端 未结 9 1259
醉酒成梦
醉酒成梦 2020-11-21 04:43

I\'m partial to using member initialization lists with my constructors... but I\'ve long since forgotten the reasons behind this...

Do you use member initialization

相关标签:
9条回答
  • 2020-11-21 05:32

    Next to the performance issues, there is another one very important which I'd call code maintainability and extendibility.

    If a T is POD and you start preferring initialization list, then if one time T will change to a non-POD type, you won't need to change anything around initialization to avoid unnecessary constructor calls because it is already optimised.

    If type T does have default constructor and one or more user-defined constructors and one time you decide to remove or hide the default one, then if initialization list was used, you don't need to update code if your user-defined constructors because they are already correctly implemented.

    Same with const members or reference members, let's say initially T is defined as follows:

    struct T
    {
        T() { a = 5; }
    private:
        int a;
    };
    

    Next, you decide to qualify a as const, if you would use initialization list from the beginning, then this was a single line change, but having the T defined as above, it also requires to dig the constructor definition to remove assignment:

    struct T
    {
        T() : a(5) {} // 2. that requires changes here too
    private:
        const int a; // 1. one line change
    };
    

    It's not a secret that maintenance is far easier and less error-prone if code was written not by a "code monkey" but by an engineer who makes decisions based on deeper consideration about what he is doing.

    0 讨论(0)
  • 2020-11-21 05:33

    As explained in the C++ Core Guidelines C.49: Prefer initialization to assignment in constructors it prevents unnecessary calls to default constructors.

    0 讨论(0)
  • 2020-11-21 05:39
    // Without Initializer List
    class MyClass {
        Type variable;
    public:
        MyClass(Type a) {  // Assume that Type is an already
                         // declared class and it has appropriate 
                         // constructors and operators
            variable = a;
        }
    };
    

    Here compiler follows following steps to create an object of type MyClass
    1. Type’s constructor is called first for “a”.
    2. The assignment operator of “Type” is called inside body of MyClass() constructor to assign

    variable = a;
    
    1. And then finally destructor of “Type” is called for “a” since it goes out of scope.

      Now consider the same code with MyClass() constructor with Initializer List

      // With Initializer List
       class MyClass {
      Type variable;
      public:
      MyClass(Type a):variable(a) {   // Assume that Type is an already
                       // declared class and it has appropriate
                       // constructors and operators
      }
      };
      

      With the Initializer List, following steps are followed by compiler:

      1. Copy constructor of “Type” class is called to initialize : variable(a). The arguments in initializer list are used to copy construct “variable” directly.
      2. Destructor of “Type” is called for “a” since it goes out of scope.
    0 讨论(0)
提交回复
热议问题