4

In C++11, default member initialization was introduced. So, I just wanted to ask that why member initializer lists must still be employed in favour of these?

E.g.

If this is allowed

class apple
{
    int i = 10;
};

Why should this be used

class apple
{
    int i;
    apple(): i(10) {}
};

Are there any specific advantages?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Gaurav Pant
  • 962
  • 10
  • 30
  • 6
    Citation needed: who said that anything must be done in favour of anything else? – Kerrek SB Jan 04 '18 at 14:35
  • Would there be any "problems" when using the former method with alternative constructors (i.e. copy constructor) ? – Code Gorilla Jan 04 '18 at 15:42
  • 1
    You can choose whichever you want, or even use both at the same time. If the second version is more common, it could be because of existing code, old habits, or non-revised coding guidelines. – Bo Persson Jan 04 '18 at 15:58

3 Answers3

4

There is no "must" here, a decision would probably be made based on circumstances.

For example, if the initialized value is not a constant, or if the value must be different in each of multiple constructors it makes sense to use the initializer list.

If you have to support pre-C++11 (and this may be more common than you think on large projects) you must always use the initializer list. Corresondingly if most of your code maintainers are not familiar with C++11 features it may be better to use the longstanding mechanism rather than the inline initialization.

If using C++11 is not an issue and you're initializing to a single constant value across multiple constructors, than the initializer form probably makes more sense.

Mark B
  • 95,107
  • 10
  • 109
  • 188
2

Member initializer list can initialize a member with a constructor-specific initializer.

For example, the initializer may depend on the parameter of the constructor, e.g.

class apple
{
    int i;
    apple(int i_) : i(i_) {}
};

or alter among different constructors, e.g.

class apple
{
    int i;
    apple(foo_type) : i(0) {}
    apple(bar_type) : i(1) {}
};

Neither case can be handled by a single default member initializer.

xskxzr
  • 12,442
  • 12
  • 37
  • 77
  • And in the case no constructor is called that initialises i then it falls back to default initialisation of i. Pretty versatile. – Zebrafish Jan 04 '18 at 15:57
1

You don't have to employ member initializer lists if you don't want to. Also, you can use a mix of default member initialization and member initializer lists. It's not just one or the other. The default member initializer will be used for any member that doesn't appear in a member initializer list.

Which one you choose depends on many things.

Default member initialization is useful when a member should be always initialized with the same value. It's a convenient shorthand that increases clarity as the initial value is shown at the place of declaration.

On the other hand, if you need to initialize a member with different values determined at runtime, you can't do that with a default initializer; you have to do it in the constructor instead.

As opposed to C++, where default member initialization is a relatively recent concept, Java has had the equivalent since its inception, and programmers have discussed the merits of the two different initialization mechanisms at length, e.g. see Default constructor vs. inline field initialization.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42