C++,初始化一个成员变量,最好的方法是定义时初始化,还是在构造函数中初始化?
PHP中文网
PHP中文网 2017-04-17 15:25:50
0
2
568

感觉在构造函数中初始化会有大量的代码,如果成员变量很多的话。。。

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
小葫芦

You can refer to the "CPP Core Guidelines" recently written by Bjarne Stroustrup, which is mentioned in Article C.48:

Prefer in-class initializers to member initializers in constructors for constant initializers

The conclusion is to give priority to putting it in in-class, that is, initialize it when the members are defined. Because if there are multiple constructors, it may cause inconsistencies and some easy misunderstandings. Detailed answers can be found here.

黄舟

It depends on the specific context. Generally speaking, I suggest that the storage application be initialized when it is defined.
Other single Init function

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