Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Internally and about the generated code, is there a really difference between :

MyClass::MyClass(): _capacity(15), _data(NULL), _len(0) { } 

and

MyClass::MyClass() {   _capacity=15;   _data=NULL;   _len=0 } 

thanks...

like image 377
Stef Avatar asked Jan 03 '11 23:01

Stef


People also ask

Why should initializer lists be used rather than assigning member variables values in the constructor body?

Initialization lists allow you to choose which constructor is called and what arguments that constructor receives. If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list.

What is the difference between member variable initialization and assignment in a constructor?

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?

Initialization lists. In fact, constructors should initialize as a rule all member objects in the initialization list.

What is the advantage of using member initializer list?

The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.


2 Answers

You need to use initialization list to initialize constant members,references and base class

When you need to initialize constant member, references and pass parameters to base class constructors, as mentioned in comments, you need to use initialization list.

struct aa {     int i;     const int ci;       // constant member      aa() : i(0) {} // will fail, constant member not initialized };  struct aa {     int i;     const int ci;      aa() : i(0) { ci = 3;} // will fail, ci is constant };  struct aa {     int i;     const int ci;      aa() : i(0), ci(3) {} // works }; 

Example (non exhaustive) class/struct contains reference:

struct bb {};  struct aa {     bb& rb;     aa(bb& b ) : rb(b) {} };  // usage:  bb b; aa a(b); 

And example of initializing base class that requires a parameter (e.g. no default constructor):

struct bb {};  struct dd {     char c;     dd(char x) : c(x) {} };  struct aa : dd {     bb& rb;     aa(bb& b ) : dd('a'), rb(b) {} }; 
like image 107
stefanB Avatar answered Oct 14 '22 08:10

stefanB


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.

like image 24
templatetypedef Avatar answered Oct 14 '22 06:10

templatetypedef