C++ initialization list vs assigning values

Viewed 645

in C++, what is the difference between initialization list and assigning values in a constructer rather than the way each method looks? I mean what's the advantage of using one rather than the other and why in the given example in the slide (below) only works with initialization? (I hope if you could add some resources to it since I didn't find)

Click here to view slide: uploaded on imgur

2 Answers

Using initialization list in constructor is the one step process i.e. it initializes objects at the moment it’s declared. It calls copy constructor.

Whereas using assignment is the two-step process i.e. define the object and then assign it. Defining objects calls default constructor and then assignment calls assignment operator. Hence, expensive operations.

In C++, constant or reference data member variables of a class can only be initialized in the initialization list, not using assignment in constructor body.

Both constant and reference data member variables have property that they both must be initialized at the moment of declaration. So, there is only way to use initialization list in constructor, as initialization list initializes class member variables at the time of declaration whereas assignment if constructor body initializes data members after declaration.

There are situations where initialization of data members inside constructor doesn’t work and Initializer List must be used. Following are such cases.

  1. For initialization of non-static const data members.
#include<iostream> 
using namespace std; 

class Test { 
    const int t; 
public: 
    Test(int t):t(t) {}  //Initializer list must be used 
    int getT() { return t; } 
}; 

int main() { 
    Test t1(10); 
    cout<<t1.getT(); 
    return 0; 
}
  1. For initialization of reference 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; 
}
  1. For initialization of member objects which do not have default constructor. (In your case Array digits does 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; 
}
  1. For initialization of base class members.
  2. When constructor’s parameter name is same as data member.
  3. For Performance reasons.

If you don't use an initialization list, the data members of the class will be default constructed before the body of the constructor is reached:


class Foo{
    private:
        int bar;

    public:
        Foo(int _bar){//bar is default constructed here
            bar = _bar; //bar is assigned a new value here
        }
};

This isn't a big issue for a fundamental-type, like int, as the default constructor is not expensive. However, it can become an issue if the data member does not have a default constructor, or default construction followed by assignment is more expensive than direct construction:

//Bar does not have a default constructor, only a copy constructor
class Bar{
    public:
        Bar() = delete; //no default constructor
        Bar(const Bar& bar); //copy constructor only
};

class Foo{
    private:
        Bar bar;

    public:
        Foo(const Bar& _bar){
            //this will not compile, bar does not have a default constructor
            //  or a copy assignment operator
            bar = _bar;
        }

        Foo(const Bar& _bar) : bar(_bar){//this will compile, copy constructor for bar called
        }
};

Generally speaking, use the initialization list for more efficient code.

Related