In This Specific Case, Is There a Difference Between Using a Member Initializer List and Assigning Values in a Constructor

In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?

Assuming that those values are primitive types, then no, there's no difference. Initialization lists only make a difference when you have objects as members, since instead of using default initialization followed by assignment, the initialization list lets you initialize the object to its final value. This can actually be noticeably faster.

Is there a difference between my_constructor : variable(x) and this.variable = x in constructor?

If you have a programming background (as you said, you have), you should be well aware of the difference between initializing value to variable and assignment of a value to some variable.

Demo1 is using the variable initialization (instance_variable is being initialized in the member initializer list, Demo() : instance_variable(5)), but Demo2 is assigning value to the instance_variable

I am not sure if the two are equivalent or if Demo1 has subtle differences I should be aware of.

No. The two aren't equivalent.

  • Demo1 is initializing the instance_variable, it means copy constructor will be called (in the case, when instance_variable is object).
  • But the Demo2 is assigning value to the variable after it has been initialized (default constructor has been called, and now operator= will be called to copy values)

Also, what is the expression constructor:field(x){} called?

It's called member initializer list.

EDIT

Member initializer list only makes a difference when initializing objects, but in case of primitives, both ways (Demo1 and Demo2), they're pretty much the same.

And there's another thing that you should know, is, that you'll have to use member initializer list to initialize:

  • const data members
  • member reference variables
  • initializing those member variables (objects of some class), which don't have any default constructor.
  • and when passing parameters to base class constructor.

Initializing fields in constructor - initializer list vs constructor body

They are not the same if member1 and member2 are non-POD (i.e. non-Plain Old Data) types:

public : Thing(int _foo, int _bar){
member1 = _foo;
member2 = _bar;
}

is equivalent to

public : Thing(int _foo, int _bar) : member1(), member2(){
member1 = _foo;
member2 = _bar;
}

because they will be initialized before the constructor body starts executing, so basically twice the work is done. That also means, if the type of these members don't have default constructor, then your code will not compile.

Difference between initializer and default initializer list in c++

The purpose of default initialization list is to initialize the constant variable with in the class.
Because the constant variable is initialized before the object is initialized.

I provide one sample to explain the difference between these two initializations:

 class A
{
private:
const int x;

};

A::A():x(5) //this code works fine
{

}

A::A() //this code is wrong.const variable is not initialized once object
{
x=5;
}

What is benefit of this constructor definition

Those are constructor initialization lists, and for fundamental types there is no difference with respect to the form you are used to, which is based on assignment rather than initialization.

However, for user-defined types there might be a difference in terms of performance (and semantics perhaps) between:

  1. default-constructing an object and then assigning a value to it, and
  2. directly initializing it with that value.

Also, you do not have a choice for those types that are not default-constructible other than using a member initialization list to initialize them, and you do not have a choice for const and reference members either, that must be initialized immediately.

Why should I prefer to use member initialization lists?

For POD class members, it makes no difference, it's just a matter of style. For class members which are classes, then it avoids an unnecessary call to a default constructor. Consider:

class A
{
public:
A() { x = 0; }
A(int x_) { x = x_; }
int x;
};

class B
{
public:
B()
{
a.x = 3;
}
private:
A a;
};

In this case, the constructor for B will call the default constructor for A, and then initialize a.x to 3. A better way would be for B's constructor to directly call A's constructor in the initializer list:

B()
: a(3)
{
}

This would only call A's A(int) constructor and not its default constructor. In this example, the difference is negligible, but imagine if you will that A's default constructor did more, such as allocating memory or opening files. You wouldn't want to do that unnecessarily.

Furthermore, if a class doesn't have a default constructor, or you have a const member variable, you must use an initializer list:

class A
{
public:
A(int x_) { x = x_; }
int x;
};

class B
{
public:
B() : a(3), y(2) // 'a' and 'y' MUST be initialized in an initializer list;
{ // it is an error not to do so
}
private:
A a;
const int y;
};


Related Topics



Leave a reply



Submit