Under the C++11 standard, we can supply an in-class initializer for a
data member. When we create objects, the in-class initializers will be
used to initialize the data members. Members without an initializer
are default initialized.
Your first example uses an in-class initializer, while your second example only initializes a
within the default constructor.
Say you have another constructor z
, which takes some parameters but does not initialize data member a
. Then upon calling z
,
- If you use in-class initializer, it will be used to set
a = 3
. - If you only initialize
a
in your default constructor, thena
will be uninitialized.
1
solved Difference between in class and constructor initialisation [closed]