C++ Member Initializer

October 27, 2015 Posted by WithU Technologies
There are situations when we need to work with both constant variable and regular variable.

But during the initialization of a constructor we declare one regular variable and one const variable then the program returns error.

Example without Member initializer:

Header file (.h):

#ifndef NEWCLASS_H_    // If not Defined
#define NEWCLASS_H_    // Then Define

class NewClass {

public:

      NewClass(int x, int y);  //Constructor is declared with two parameters

private:

       int rv;     //Regular variable initialised

       const int cv;   //Constant variable initialised

};

#endif /* NEWCLASS_H_ */  //Condition ends  here

Source file (.cpp):

#include "NewClass.h"
#include <iostream>   // iostream header is included to use cout

using namespace std;   //namespace standard library is also included

NewClass::NewClass(int x, int y)

   rv = x;         //rv is a regular variable and thus x is also a regular variable

   cv = y;         //cv is a constant variable and thus y is also an constant variable

      {

      cout<<y<<endl;  

      cout<<x<<endl;

    }

// Output: It gives an error as constant variable cannot have assigned a value after declaration

Main (.cpp)

#include <iostream>
#include "NewClass.h"   // Include our classes to the main function

using namespace std;

int main()

{

      NewClass NewOb(12,23);  //New constant Object is created which passes two value to the function parameter.

                              //12 will be passed as regular variable and 23 will be passed as constant variable

}

·         So, ultimately it returns an error during compilation process because one of its member variable is constant which cannot be assigned a value after declaration.

·         Here we need the Member initializer.

Member Initializer:

Format:

: variable(value), variable(value)…..
·         It follows the constructor parameter and according to that the list generated.

·         It acts as a list where variables are stored with value separated by comma.

·         It does not ends with a semi colon. But it begins with a colon (:).

Example with Member initializer:

Rest of the files are remains same except the source file where we define how the function work. So, the new source fill will be:

#include "NewClass.h"
#include <iostream>   // iostream header is included to use cout

using namespace std;   //namespace standard library is also included

NewClass::NewClass(int x, int y)

   :rv(x),cv(y)   // Member initializer list will be initialized

      {

      int sum;

      cout<<"The first number is: "<<x<<endl;

      cout<<"The second number is: "<<y<<endl;

      sum = x+y;

      cout<<"The sum will be: "<<sum<<endl;

    }
/*

 * Output:

The first number is: 12

The second number is: 23

The sum will be: 35*/

·         As we can see that a Member initializer list is initialized. Two variables along with their values are listed there.

·         The first one is a regular variable and the second one is constant variable along with their values.

·         We assign two parameters to the member variables via the member initialization list.

·         Member initialization list may be used for regular variables and must be used for constant variables.

·         It is a good practice to use member initializer syntax even when the variables are not constant.

·         A constant member variable must be initialized in the constructor initialization list.