Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - initializing variables in header vs with constructor

Regarding the following, are there any reasons to do one over the other or are they roughly equivalent?

class Something {     int m_a = 0; }; 

vs

class Something {     int m_a;     Something(int p_a); };  Something::Something(int p_a):m_a(p_a){ ... }; 
like image 550
Evan Ward Avatar asked Feb 09 '15 15:02

Evan Ward


People also ask

Should I initialize variable within constructor or outside constructor?

If you know what value a final variable will have at declaration, it makes sense to initialize it outside the constructors. However, if you want the users of your class to initialize the final variable through a constructor, delay the initialization until the constructor.

Do you have to initialize all variables in constructor?

You should always initialize native variables, especially if they are class member variables. Class variables, on the other hand, should have a constructor defined that will initialize its state properly, so you do not always have to initialize them.

Can we initialize variable in header file?

Yes. Although this is not necessarily recommended, it can be easily accomplished with the correct set of macros and a header file. Typically, you should declare variables in C files and create extern definitions for them in header files.

What is the difference between member variables 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.


2 Answers

The two code snippets you posted are not quite equal.

class Something {     int m_a = 0; }; 

Here you specify the value with which to initialise, i.e. 0, at compile time.

class Something {     int m_a;     Something(int p_a); };  Something::Something(int p_a):m_a(p_a){ ... }; 

And here you do it at run time (or possibly at run time), with the value p_a not known until the constructor is called.

The following piece of code comes closer to your first example:

class Something {     int m_a;     Something(); };  Something::Something() : m_a(0) { /* ... */ }; 

What you have to consider here is that in the first case, the value appears directly in the class definition. This may create an unnecessary dependency. What happens if you need to change your 0 to 1 later on? Exposing the value directly in the class definition (and thus, usually, in a header file) may cause recompilation of a lot of code in situations where the other form of initialisation would avoid it, because the Something::Something() : m_a(0) part will be neatly encapsulated in a source file and not appear in a header file:

// Something.h - stable header file, never changed class Something {     int m_a;     Something(); };  // Something.cpp - can change easily Something::Something() : m_a(0) { /* ... */ }; 

Of course, the benefits of in-class initialisation may vastly outweigh this drawback. It depends. You just have to keep it in mind.

like image 71
Christian Hackl Avatar answered Sep 19 '22 07:09

Christian Hackl


The first form is more convenient if you have more than one constructor (and want them all to initialise the member in the same way), or if you don't otherwise need to write a constructor.

The second is required if the initialiser depends on constructor arguments, or is otherwise too complicated for in-class initialisation; and might be better if the constructor is complicated, to keep all the initialisation in one place. (And it's also needed if you have to support pre-C++11 compilers.)

like image 39
Mike Seymour Avatar answered Sep 19 '22 07:09

Mike Seymour