Q:

When do we use the Initializer List in C++?

0

When do we use the Initializer List in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

In the above question, we had seen, what is the initializer list in C++. Now let us see the situation where we have to use the Initializer List in C++.

1. In the initialization of reference members:

A reference member must be initialized using Initializer List.

#include<iostream>
using namespace std;
class Test
{
    int &m_rData;
public:
    //Initializer list must be used
    Test(int & rData):m_rData(rData) {}
    int getData()
    {
        return m_rData;
    }
};
int main()
{
    int data = 27;
    Test obj(data);
    cout<<"m_rData is " << obj.getData()<<endl;
    data = 06;
    cout<<"m_rData is " << obj.getData()<<endl;
    return 0;
}

Output:

m_rData is 27
m_rData is 6

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

2. In the initialization of non-static const data members:

const data members must be initialized using Initializer List.

#include<iostream>
using namespace std;
class Test
{
    const int m_data;
public:
    //Initializer list must be used
    Test(int x):m_data(x) {}
    int getData()
    {
        return m_data;
    }
};
int main()
{
    int data = 27;
    Test obj(data);
    cout<<"m_data is " << obj.getData()<<endl;
    return 0;
}

Output: m_data is 27

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

3. In the initialization of member objects which do not have default constructor:

See the below example, an object “a” of class “A” is a data member of class “B”, and “A” doesn’t have a default constructor. Initializer List must be used to initialize “a”.

#include <iostream>
using namespace std;
//Class A
class A
{
    int i;
public:
    A(int );
};
//Class A constructor
A::A(int arg)
{
    i = arg;
    cout << "A's Constructor called: Value of i: " << i << endl;
}
//Class B
class B
{
//obj of class A
    A a;
public:
    B(int );
};
//Class B constructor.
//Initializer list must be used for a
B::B(int x):a(x)  
{
    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

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

4. In the initialization of base class members :

You have to initialize the base class members using the initialization list.

#include <iostream>
using namespace std;
//Class A
class A
{
    int i;
public:
    A(int );
};
//Class A constructor
A::A(int arg)
{
    i = arg;
    cout << "A's Constructor called: Value of i: " << i << endl;
}
//Class B
class B
{
//obj of class A
    A a;
public:
    B(int );
};
//Class B constructor.
//Initializer list to initialize base class member
B::B(int x):a(x)
{
    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

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

5. When the constructor’s parameter name is the same as the data member:

If the constructor’s parameter name is the same as the data member name then the data member must be initialized either using this pointer or Initializer List.

#include <iostream>
using namespace std;
class Test
{
    //member name same as class constructor parameter
    int data;
public:
    Test(int data):data(data) { }
    
    int getData() const
    {
        return data;
    }
};
int main()
{
    Test obj(27);
    
    cout<<obj.getData();
    
    return 0;
}

Output: 27

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

6. To increase performance:

It is better to initialize all class variables in the Initializer List instead of assigning values inside the constructor body.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is a copy constructor in c++?... >>
<< What is the initializer list in C++?...