C++ class 初始化赋值问题?
伊谢尔伦
伊谢尔伦 2017-04-17 13:22:50
0
1
431

第一个有自动生成默认构造函数吗?
为什么第二个不可以编译通过?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
大家讲道理

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template