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
The
class 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
cannot be used
NoDefault y = {2, 2};
Initialized using
.
Note: According to aggregate initialization, this condition will be deleted in C++17.
Yes, when there is no any user-defined constructor in a class, the compiler will generate a default constructor. See Default constructors.
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
TheIn the second example class definition of the subject, both members
cannot be used Initialized usinga
andb
use in-class initializer, so they are not aggregate class, so.
Note: According to aggregate initialization, this condition will be deleted in C++17.