Initializer List

 


(a) Initializer List


When do we use Initializer List in C++?




Initializer List is used in initializing the data members of a class. 

The list of members to be initialized is indicated with constructor as a comma-separated list followed by a colon. 

Following is an example that uses the initializer list to initialize x and y of Point class.


--------------------------------------------------------------

#include<iostream> 

using namespace std; 


class Point { 

private: 

int x; 

int y; 

public: 

Point(int i = 0, int j = 0):x(i), y(j) {} 

/* The above use of Initializer list is optional as the 

constructor can also be written as: 

Point(int i = 0, int j = 0) { 

x = i; 

y = j; 

*/

int getX() const {return x;} 

int getY() const {return y;} 

}; 


int main() { 

Point t1(10, 15); 

cout<<"x = "<<t1.getX()<<", "; 

cout<<"y = "<<t1.getY(); 

return 0; 


/* OUTPUT: 

x = 10, y = 15 

*/

--------------------------------------------------------------------


(a) For initialization of non-static const data members:

(b)  For initialization of reference members:

(c) For initialization of member objects which do not have default constructor:

(d)For initialization of base class members 

(e) When constructor’s parameter name is same as data member

(f) For Performance reasons:




(a) For initialization of non-static const data members:

(b)  For initialization of reference members:

================================================================

// Initialization of reference data members 

#include<iostream> 

using namespace std; 


class Test { 

int &t; 

public: 

Test(int &t):t(t) {} //Initializer list must be used 

int getT() { return t; } 

}; 


int main() { 

int x = 20; 

Test t1(x); 

cout<<t1.getT()<<endl; 

x = 30; 

cout<<t1.getT()<<endl; 

return 0; 

/* OUTPUT: 

20 

30 

*/

======================================================================

(c) For initialization of member objects which do not have default constructor:



============================================================

#include <iostream> 

using namespace std; 

class A { int i; 

       public: 

  A(int ); }; 


A::A(int arg) { 

i = arg; 

cout << "A's Constructor called: Value of i: " << i << endl; 

   } 


// Class B contains object of A 

       class B { 

      A a; 

      public: 

  B(int ); 

   }; 


B::B(int x):a(x) { //Initializer list must be used 

cout << "B's Constructor called"; 

         } 


int main() { 

B obj(10); 

return 0; 

/* OUTPUT: 

A's Constructor called: Value of i: 10 

B's Constructor called 

*/

========================================================================








(d)For initialization of base class members 

================================================

#include <iostream> 

using namespace std; 

class A { 

int i; 

    public: 

A(int ); 

}; 


A::A(int arg) { 

          i = arg; 

         cout << "A's Constructor called: Value of i: " << i << endl; 

       } 


// Class B is derived from A 

class B: A { 

         public: 

  B(int ); 

        }; 


B::B(int x):A(x) { //Initializer list must be used 

cout << "B's Constructor called"; 

       } 


int main() { 

B obj(10); 

return 0; 

=======================================================



(e) When constructor’s parameter name is same as data member

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

==================================================

#include <iostream> 

using namespace std; 


class A { 

int i; 

public: 

A(int ); 

int getI() const { return i; } 

}; 


A::A(int i):i(i) { } // Either Initializer list or this pointer must be used 

/* The above constructor can also be written as 

A::A(int i) { 

this->i = i; 

*/


int main() { 

A a(10); 

cout<<a.getI(); 

return 0; 

/* OUTPUT: 

10 

*/

=====================================================



(f) For Performance reasons:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


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; 

3. And then finally destructor of “Type” is called for “a” since it goes out of scope.












Comments

Popular posts from this blog

Difference between Structure and Array in C

new operator