[英]Class POD member variable initialization
Regarding the following two ways to initialize the POD member members variables: 关于以下两种初始化POD成员成员变量的方法:
class Trie {
Trie()
: isWord_(false)
{ }
vector<Trie *> child(keyLength);
bool isWord;
};
// OR
class Trie {
Trie();
vector<Trie *> child(keyLength);
bool isWord = false;
};
Are the two equivalent (performance wise, generated code size wise, etc)? 这两个是否相等(在性能方面,在生成代码大小方面等)? Is there a current preference? 当前有偏好吗?
I would refer to this article Get to Know the New C++11 Initialization Forms for answers. 我将参考本文“了解新的C ++ 11初始化表格”以获取答案。
They are semantically equivalent, as the article states: 正如文章所述,它们在语义上是等效的:
Regardless of the initialization form used, the compiler conceptually transforms every class member initializer into a corresponding mem-init 无论使用哪种初始化形式,编译器都会在概念上将每个类成员初始化程序转换为相应的mem-init
However there are some things that are allowed by second option which are impossible for first one if we go outside of POD scope. 但是,如果我们超出了POD范围,则第二个选项允许有些事情对于第一个选项是不可能的。 For example: array initialization. 例如:数组初始化。
As for current preference, there is always a strong argument of making Your code readable for others: 至于当前的偏好,总是有一个很强的论据可以使您的代码对其他人可读:
My personal preference would still be first option, because with it I have all the information about class initialization in one place (the constructor) and don't have to scroll all the way to the bottom to see if someone suddenly made a default initialization there. 我个人的喜好仍然是第一选择,因为有了它,我就可以将所有有关类初始化的信息都放在一个地方(构造函数),而不必一直滚动到最底部以查看是否有人突然在那里进行了默认初始化。 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.