Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor initialization Vs assignment

Let us consider the following classes

class test1 {     private:         int a;         int b;     public:         test1():a(0),b(0){} };  class test2 {     private:         int a;         int b;     public:         test2()         {             a=0;             b=0;         } }; 

Now, I know that test1() constructor is the right way to initialize the data members of a class, because in test2() we are performing assignment and not initialization. My questions are:

  1. What might go wrong if we perform assignment instead of initialization?
  2. Doesn't the compiler internally perform assignment in case of test1() constructor? If not, then how are these initialized?
like image 769
Uttam Malakar Avatar asked Mar 28 '13 10:03

Uttam Malakar


People also ask

What is the difference between initialization and assignment?

Initialization gives a variable an initial value at the point when it is created. Assignment gives a variable a value at some point after the variable is created.

Should my constructors use initialization lists or assignment?

Should my constructors use “initialization lists” or “assignment”? ¶ Δ Initialization lists. In fact, constructors should initialize as a rule all member objects in the initialization list.

What is initialization in constructor?

Explicit initialization with constructors (C++ only) A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.

Is constructor same as initializer?

Constructor is a special non-static member function of a class that is used to initialize objects of its class type. In the definition of a constructor of a class, member initializer list specifies the initializers for direct and virtual bases and non-static data members.


2 Answers

What might go wrong if we perform assignment instead of initialization?

Some class types (and also references and const objects) can't be assigned; some can't be default-initialised; some might be more expensive to default-initialise and reassign than to initialise directly.

Doesn't the compiler internally performs assignment in case of test1() constructor? If no then how are these initialized?

In the case of primitive types like int, there is little or no practical difference between the two. Default-initialisation does nothing, and direct-initialisation and assignment both do essentially the same thing.

In the case of class types, default-initialisation, assignment and direct-initialisation each call different user-defined functions, and some operations may not exist at all; so in general the two examples could have very different behaviour.

like image 119
Mike Seymour Avatar answered Oct 23 '22 13:10

Mike Seymour


For your example there is no real different because you are initializing plain integers.

But assume these integers are objects with constructors, then the compiler would generate the following calls:

// test1 a::copy_constructor(0); b::copy_constructor(0);   // test2 a::default_constructor(); b::default_constructor(); a::operator = (0); b::operator = (0); 

So depending on your objects test2 could have a huge performance impact. Also by initializing your objects in the initializing lists guaranties that your variables have the data when you enter the constructor. One 'drawback' of the initializer list is that the it is executed in the order that the variables are declared and not in the order of the initializer list, so it could be that you don't want to use the initializer list.

like image 25
ConfusedSushi Avatar answered Oct 23 '22 15:10

ConfusedSushi