1

I've been away from C++ for awhile and I am trying to determine the difference between these two default constructor initializations of a defined class's private member variables (if any). The class definition is in a header file while the implementation is in a separate file. All private member variables need to be initialized to "0" or a non-meaningful value.

NOTE:

int data[MAX_SIZE] is a fixed size compile time array. int used: is an index tracker for the array.

Generally speaking, I'm accustomed to initializing members in the following way:

 // Default Constructor
 IntSet::IntSet()
 {
    data[MAX_SIZE-1] = {0}
    used = 0;
 }

I'm combing through some legacy code and finding a completely different syntax:

 // Default Constructor
 IntSet::IntSet() : used(0)
 {
    data[MAX_SIZE-1] = {0};
 }

Is there a difference in the end result?

  • Prefer [initializer lists](http://en.cppreference.com/w/cpp/language/initializer_list) as in your second snippet. VLAs are not allowed in C++. – Ron Sep 05 '17 at 02:47
  • No VLA in use here. All that's doing is setting the last value of `data` to 0. – user4581301 Sep 05 '17 at 02:49

1 Answers1

5

Members are initialized when they are constructed, before the body of the constructor is reached. There are cases where they differ. For example:

  • if a member is const or a reference, it must be initialized in the initializer list, and cannot be initialized in the constructor body

  • if the member has no default constructor (or you don't have access to it), then you must provide the constructor it should use some arguments, and that can only be done in the initializer list.

  • if the object is "big and expensive" to create and also to assign to, then you could be doing something inefficiently by default constructing the object and then using the assignment operator on it. (Rather than constructing it with its initial value at the same time.)

For integers and primitive, POD types like you described default construction is trivial and so either way is equivalent, given the above restrictions on reference, const, etc.

whoan
  • 8,143
  • 4
  • 39
  • 48
Chris Uzdavinis
  • 6,022
  • 9
  • 16
  • Addendum: More reading on the [Member Initializer List](http://en.cppreference.com/w/cpp/language/initializer_list) – user4581301 Sep 05 '17 at 02:57
  • Thanks for the explanations and references to other readings. Really appreciate it! Is there a better way to initialize every element of the array to a NULL value while also setting "used" to "0"? – Alexander Maxwell Sep 05 '17 at 03:59
  • @AlexanderMaxwell you can't initialize the array in constructor initializer list. For that, you'll have to use for loop or equivalent loop for initializing each element to NULL. – Plochie Sep 05 '17 at 04:23
  • Since I don't care what the value in the array is as long as it's initialized could I do this: // Default Constructor IntSet::IntSet() : used(0), data(){} – Alexander Maxwell Sep 05 '17 at 05:49
  • @PareshLomate That was true in c++03, but after c++11, that's no longer correct. Arrays *can* be initialized in the constructor initializer list. – Chris Uzdavinis Sep 05 '17 at 14:42