Why should I prefer to use member initialization lists?

后端 未结 9 1480
醉酒成梦
醉酒成梦 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:25

    Syntax:

      class Sample
      {
         public:
             int Sam_x;
             int Sam_y;
    
         Sample(): Sam_x(1), Sam_y(2)     /* Classname: Initialization List */
         {
               // Constructor body
         }
      };
    

    Need of Initialization list:

     class Sample
     {
         public:
             int Sam_x;
             int Sam_y;
    
         Sample()     */* Object and variables are created - i.e.:declaration of variables */*
         { // Constructor body starts 
    
             Sam_x = 1;      */* Defining a value to the variable */* 
             Sam_y = 2;
    
         } // Constructor body ends
      };
    

    in the above program, When the class’s constructor is executed, Sam_x and Sam_y are created. Then in constructor body, those member data variables are defined.

    Use cases:

    1. Const and Reference variables in a Class

    In C, variables must be defined during creation. the same way in C++, we must initialize the Const and Reference variable during object creation by using Initialization list. if we do initialization after object creation (Inside constructor body), we will get compile time error.

    1. Member objects of Sample1 (base) class which do not have default constructor

       class Sample1 
       {
           int i;
           public:
           Sample1 (int temp)
           {
              i = temp;
           }
       };
      
        // Class Sample2 contains object of Sample1 
       class Sample2
       {
        Sample1  a;
        public:
        Sample2 (int x): a(x)      /* Initializer list must be used */
        {
      
        }
       };
      

    While creating object for derived class which will internally calls derived class constructor and calls base class constructor (default). if base class does not have default constructor, user will get compile time error. To avoid, we must have either

     1. Default constructor of Sample1 class
     2. Initialization list in Sample2 class which will call the parametric constructor of Sample1 class (as per above program)
    
    1. Class constructor’s parameter name and Data member of a Class are same:

       class Sample3 {
          int i;         /* Member variable name : i */  
          public:
          Sample3 (int i)    /* Local variable name : i */ 
          {
              i = i;
              print(i);   /* Local variable: Prints the correct value which we passed in constructor */
          }
          int getI() const 
          { 
               print(i);    /*global variable: Garbage value is assigned to i. the expected value should be which we passed in constructor*/
               return i; 
          }
       };
      

    As we all know, local variable having highest priority then global variable if both variables are having same name. In this case, the program consider "i" value {both left and right side variable. i.e: i = i} as local variable in Sample3() constructor and Class member variable(i) got override. To avoid, we must use either

      1. Initialization list 
      2. this operator.
    

提交回复
热议问题