Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Initialization List in C++ (http://www.go4expert.com/articles/initialization-list-cpp-t20221/)

Mridula 28Nov2009 18:37

Initialization List in C++
 

Introduction



This article talks about Initialization List in C++.

Background



Definition:

Initialization List is another or a better approach for initializing the member variables (and base class objects) of a class upon construction of an instance of it's own.

Things to know about Initialization List
  • Initialization List is used to initialize both user defined data types (like embedded object of a class) and also primitive/built-in data types (like int, char).
  • Initialization List can appear irrespective of the place the constructor is defined.
  • Initializing the member variables in the Initialization List is better than initializing them inside the body of the constructor of the class.
  • Data members are initialized in the order they are declared, regardless of the order of their initialization.
  • It is mandatory to initialize Reference Data Member in an Initialization List because it can not exist without being initialized.
  • It is mandatory to initialize Constant Data Member in an Initialization List otherwise it would be constrcuted with some junk values and we cannot initialize it later anywhere else.
  • It is mandatory to construct and initialize, embedded class objects/base class objects in case of inheritance, in an Initialization List, if they do not themselves have a zero-argument/default constructor provided.

Initializing User Defined Data Types and Primitive/Built-in Data Types

Primitive Data Type

Giving an argument to a Primitive Data as shown below in the example is nothing but assigning a value to it.

The code


Code: Cpp

#include<iostream.h>

class A
{
  int a;
  char b;

  public:
    A():a(100),b('m')
    {
      cout<<"Value of a:"<<a<<endl;
      cout<<"Value of b:"<<b<<endl;
    }
};

int main()
{
  A a;
  return(0);
}

Output
--------
./a.out
Value of a:100
Value of b:m

Example for Initializing User Defined Data Type

The code



Code: Cpp

#include<iostream.h>

class B
{
  int x;

  public:
    B(int data):x(data)
    {};
    void print()
    {cout<<"Value of x is: "<<x<<endl;}
};

class A
{
   int a;
   B b;

  public:
    A():a(100),b(a)
    {
      cout<<"Value of a:"<<a<<endl;
      b.print();
    }
};

int main()
{
  A a;
  return(0);
}

Output
-------
./a.out
Value of a:100
Value of x is: 100

Initialization List can appear irrespective of the place the constructor is defined

Initialization List, when the constructor definition is inline, inside the class body.

For Example:
Code: Cpp

class Rect{
private:
 int width_m, height_m;
public:
 Rect(int w, int h) :width_m(w), height_m(h) //initialization list is here
 {
   cout<<"Ctor...\n";
 }
};

Initialization List, when the constructor definition is outside the class body.

For Example:
Code: Cpp

class Rect{
private:
 int width_m, height_m;
public:
 Rect(int w, int h);
};

Rect(int w, int h) :width_m(w), height_m(h) //initialization list is here
{
  cout<<"Ctor...\n";
}

Initializing the member variables in the Initialization List is better than initializing them inside the body of the constructor of the class.

There are two steps that takes place when Member Objects of a class are initialized inside the body of a constructor.
  1. Member Objects are allocated a memry/constructed and are given default values by the time when the control enters body of the constructor.
  2. Later on the actual initialization happens inside the body of the constructor i.e. user written initilization code that gets called.

Here there's an unnecessary first step where the member objects of a class gets constructed and are given a default value. An initialization list avoids this step and can make your code execute faster as a result.

Data members are initialized in the order they are declared, regardless of the order of their initialization.

It is important to know that the order of initializing the member variables in the Initialization List must match the order of their declarations inside the class. The reason is that the compiler automatically transforms the Initialization List so that it coincides with the order of the declaration of class members inside the class.

For Example

Case 1:

The code



Code: Cpp

#include<iostream.h>

class A
{
  int a;
  int b;

  public:
    A():b(0),a(b)
    {
      cout<<"Value of b:"<<b<<endl;
      cout<<"Value of a:"<<a<<endl;
    }
};

int main()
{
  A a;
  return(0);
}

./a.out
Value of b:100
Value of a:-4265828

Case 2:

The code



Code: Cpp

class A
{
  int a;
  int b;

  public:
    A():a(100),b(a)
    {
      cout<<"Value of b:"<<b<<endl;
      cout<<"Value of a:"<<a<<endl;
    }
};

int main()
{
  A a;
  return(0);
}

./a.out
Value of b:100
Value of a:100

With respect to the above point, as the varible a is declared earlier than b, it is (i.e. variable a) initialized earlier than varible b. So, in the Case 1, initialization of a(b) lead to junk initialization as b was not yet initialized to 100 then.

Reference Data Member

Though it is an option for initializing other member variables of a class in an Initialization List, but it is must to initialize Reference data members in an Initialization List.

For Example:

The code



Code: Cpp

#include<iostream.h>

class A{
  int &a;
  int b;
    public: A():a(b), b(10){cout<<"Value of reference var a :"<<a<<endl;};
};

int main()
{
  A a;
  return(0);
}

In the above code, compiler throws an error, if reference variable a is not initialized in the Initialization List.

Constant Data Member

Constant Data members must be initialized in an Initialization List, else they will be default initialized with some junk values.

The code



Code: Cpp

#include<iostream.h>

class A{
    const int a;
    const int b;
public:
    A::A(){};
    void print(){cout<<"a = "<<a<<endl;
                 cout<<"b = "<<b<<endl;};
};

int main()
{

A a;
a.print();

return (0);
}

./a.out
a = 4
b = -4265828

As shown in the above code, constant data members a and b are not not initialized in an Initialization List and so they are by default initialized with some junk value.

No Default Constructor

If class has an embedded class object that has no default constructor or if the class is derived from another class which has no default constructor, then we must specify which constructor we wish to use while constructing and initializing the object of that class, in an Initialization List of our class's constructor.

If we take the same example that is used for User Defined Data Type above, the class B is a member variable of class A and has no default constructor and if we are not initializing the class B's instance in class A's constructor's initialization List, then compiler will throw the below error.

initList3.cc: In constructor `A::A()':
initList3.cc:21: error: no matching function for call to `B::B()'
initList3.cc:4: error: candidates are: B::B(const B&)
initList3.cc:8: error: B::B(int)


Hope this helps to understand the importance of an Initialization List in C++.

shabbir 7Dec2009 09:25

Re: Initialization List in C++
 
Nominate this article for Article of the month - Nov 2009

technica 18Dec2009 12:21

Re: Initialization List in C++
 
Nice article with good code examples.

shabbir 21Dec2009 10:51

Re: Initialization List in C++
 
Vote this article for Article of the month - November 2009

Prathy 14Jul2011 14:15

Re: Initialization List in C++
 
Detail explanation...thank you :pleased:

anoopseo 21Jul2011 12:40

Re: Initialization List in C++
 
thank you 4 your article.............

hagar_mohammed 10Nov2012 23:20

Re: Initialization List in C++
 
thank you for a good explain


All times are GMT +5.5. The time now is 06:08.