简体   繁体   English

我是否应该始终在C ++中初始化类的每个数据成员?

[英]Should I always initialize every data member of a class in c++?

Is this a good practise,or it just depends? 这是一个好习惯,还是取决于?

Thanks. 谢谢。

The only time it might make sense not to initialize would be if you are going to default-construct large numbers of POD objects in performance-critical code and then populate them with valid data afterwards -- eg if you were going to create an array of one million objects, and then populate the array's objects with valid data. 唯一不有意义的初始化是,如果您要使用性能关键代码默认构造大量POD对象,然后再使用有效数据填充它们-例如,如果要创建一个数组一百万个对象,然后使用有效数据填充数组的对象。 In that scenario you might want to avoid the initialization, since it's a waste of CPU cycles to zero everything out when you're only to overwrite it again afterwards. 在那种情况下,您可能要避免初始化,因为在以后仅再次覆盖它时,将所有内容清零会浪费CPU周期。

But if you do that, be sure to put /* LOTS OF EYE-GRABBING COMMENTS */ in your class's .h file warning the user about what you are doing and why, and document it thoroughly in any other programmer's documentation you maintain. 但是,如果要这样做,请确保在类的.h文件中添加/ *很多令人眼花COM乱的注释* /,以警告用户您正在做什么以及为什么这样做,并在所有您维护的程序员文档中彻底记录下来。 Tracking down uninitialized-value bugs isn't much fun... 跟踪未初始化值的错误不是很有趣...

The rule of thumb is an object should never exist in an uninitialized state. 经验法则是,对象永远不应该处于未初始化状态。 The only way to accomplish this is to initialize all member variables during construction. 实现此目的的唯一方法是在构造过程中初始化所有成员变量。 There is a difference between an object being in an uninitialized state and an invalid state. 对象处于未初始化状态和无效状态之间是有区别的。 An uninitialized state occurs when member variables are not initialized object during construction. 当成员变量在构造期间不是初始化对象时,将发生未初始化状态。 Since the variables likely contain unknown values any member function that relies on them may intermittently behave differently. 由于变量可能包含未知值,因此依赖它们的任何成员函数都可能间歇性地表现不同。 An invalid state initializes the variables to default values providing some expectation the object will always behave the same after construction. 无效状态会将变量初始化为默认值,前提是可以期望对象在构造后始终表现出相同的行为。

In the book: "Ruminations on C++" by Andrew Koenig and Barbara Moo, Chapter 4 在书中:安德鲁·科尼希(Andrew Koenig)和芭芭拉·穆(Barbara Moo)撰写的“ C ++上的思考”,第4章

Does every ctor need to init every data member? 每个ctor是否都需要初始化每个数据成员? It's not always true, sometimes your data member has it's meaning when your object exsits for a while, it really depends. 这并不总是正确的,有时您的数据成员具有它的含义,即当对象存在一段时间时,它确实取决于。

The book I have is not the original English version, so I kind of translated it. 我拿着的书不是原始的英文版,所以我有点翻译了。 If you want to know more, read the whole Chapter4:) 如果您想了解更多,请阅读整章第4章:)

Never say always. 永远不要说永远。

It is a very, very good pratice to initialize every data member. 初始化每个数据成员是一个非常非常好的实践。 But sometimes, in very specific situations, you may need to skip some initialization if you are going to create a large amount of objects - which will take more CPU cycles than you would like - and you will initialize them later, and you can garantee that no data member will be ever used before initialized. 但是有时,在非常特殊的情况下,如果要创建大量对象,则可能需要跳过一些初始化操作-这将花费比您想要的更多的CPU周期-然后您将对其进行初始化,因此可以保证初始化之前不会使用任何数据成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM