大家讲道理2017-04-17 13:24:50
Does the first one automatically generate a default constructor?
Yes, when there is no any user-defined constructor in a class, the compiler will generate a default constructor. See Default constructors.
Why doesn’t the second one compile?
If a class wants to use a brace list to initialize members, then the class must meet the conditions of aggregate class. Among the conditions of aggregate class, one is
Theclass does not contain any in-class initializer.
In the second example class definition of the subject, both members a
and b
use in-class initializer, so they are not aggregate class, so
NoDefault y = {2, 2};
Initialized using .
Note: According to aggregate initialization, this condition will be deleted in C++17.