eclipse 构造函数应该初始化类的所有数据成员吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33124542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:54:36  来源:igfitidea点击:

Should constructor initialize all the data members of the class?

c++eclipseinitializationlanguage-lawyerinitializer-list

提问by gsamaras

I have a situation like this:

我有这样的情况:

class A {
public:
  A() : n(0) {}
private:
  int n;
  int m;
}

There is simply no meaning in the application logic to initialize min the constructor. However, Eclipse warns me that the constructor leaves muninitialized. I can't run the code somewhere else now. The warning is:

在应用程序逻辑m中在构造函数中初始化没有任何意义。但是,Eclipse 警告我构造函数m未初始化。我现在无法在其他地方运行代码。警告是:

Member 'm' was not initialized in this constructor

成员“m”未在此构造函数中初始化

So, does C++ encourage us to initialize all the data members in the constructor or it is just Eclipse's logic?

那么,是 C++ 鼓励我们在构造函数中初始化所有数据成员还是只是 Eclipse 的逻辑?

采纳答案by eerorika

Should constructor initialize all the data members of the class?

构造函数应该初始化类的所有数据成员吗?

That would be a good practice.

那将是一个很好的做法。

So, does C++ encourage us to initialize all the data members in the constructor?

那么,C++ 是否鼓励我们在构造函数中初始化所有数据成员?

It's not required by the c++ standard. As long as you initialize all variables before they're used, your program is correct in that regard.

C++ 标准不需要它。只要您在使用之前初始化所有变量,您的程序在这方面就是正确的。

or it is just Eclipse's logic?

或者这只是 Eclipse 的逻辑?

Quite likely. Neither g++ nor clang versions that I tested warn about this when all warnings are enabled. The logic may or might not be based on high integrity c++ coding standard 12.4.2or some other coding standard or style guide.

很有可能。当启用所有警告时,我测试的 g++ 和 clang 版本都不会对此发出警告。该逻辑可能基于也可能不基于高完整性 c++ 编码标准 12.4.2或其他一些编码标准或样式指南。

回答by toubab

C++ doesn't require attributes to be initialized in constructor, except in case of const attributes where there value must be defined in initialization list.

C++ 不需要在构造函数中初始化属性,除非在必须在初始化列表中定义值的 const 属性的情况下。

However, it is clearly a good practice to initialize every attributes in constructor. I cannot count how many bugs I've met due to a non initialized variable or attributes.

但是,在构造函数中初始化每个属性显然是一个好习惯。由于未初始化的变量或属性,我无法计算我遇到了多少错误。

Finally, every object should permanentlybe in a consistentstate, which include both public (accessible) attributes and private attributes as well. Optimization should not be a reason for keeping an object un-consistent.

最后,每个对象都应该永久处于一致状态,这包括公共(可访问)属性和私有属性。优化不应成为保持对象不一致的原因。

回答by Jonah Graham

For completeness, the warning comes from the C/C++ Code Analysis. In particular the problem is Potential Programming Problems/ Class members should be properly initialized

为完整起见,警告来自 C/C++ 代码分析。特别是问题是Potential Programming Problems/Class members should be properly initialized

To change the code analysis settings (in this case I recommend per-project) edit the project properties. You can disable the whole warning, or disable it on just the files that violate the warning condition.

要更改代码分析设置(在这种情况下,我建议每个项目)编辑项目属性。您可以禁用整个警告,或仅对违反警告条件的文件禁用它。

show the warning

显示警告

As for comparing CDT with GCC or CLang, this appears to be a case where additional code analysis is being done by CDT compared to what is available from the compilers. Of course that is to be expected as the CDT Code Analysis' remit is greater than that of the compiler.

至于将 CDT 与 GCC 或 CLang 进行比较,这似乎是一种情况,与编译器提供的内容相比,CDT 正在执行额外的代码分析。当然,这是意料之中的,因为 CDT 代码分析的职权范围大于编译器的职权范围。

PS, If you are up for it, you can read the implementation of this particular checker.

PS,如果您愿意,可以阅读此特定检查器的实现。

回答by SergeyA

Fully disagree with all the answers and comments. There is absolutely no need to default initialze a member when it is not needed. This is why C/C++ never initializes built-in types as members or automatic variables - because doing so would impede performance. Of course, it is not a problem when you create your object/variable once (that's why statics are default-initialized), but for something happening in a tight loop default initialization might eat valuable nanoseconds.

完全不同意所有的答案和评论。绝对不需要在不需要时默认初始化成员。这就是 C/C++ 从不将内置类型初始化为成员或自动变量的原因——因为这样做会影响性能。当然,当您创建对象/变量一次时这不是问题(这就是静态默认初始化的原因),但是对于在紧密循环中发生的某些事情,默认初始化可能会消耗宝贵的纳秒。

The one exception to this rule would, in my view, be pointers (if you happen to have raw pointers in your code). Raw pointers should be NULL-initialized, since having invalid pointer is a direct way to undefined behaviour.

在我看来,这个规则的一个例外是指针(如果你的代码中碰巧有原始指针)。原始指针应该是 NULL 初始化的,因为无效指针是导致未定义行为的直接方式。

回答by rockefelox

As it has been already said, you should always initialize pointers and of course const objects are mandatory.

正如已经说过的,您应该始终初始化指针,当然 const 对象是强制性的。

In my opinion you should not initialize when it is not necessary but it is good to check for all non constructor initialized variables once in a while because they are source of very frequent and hard to find bugs.

在我看来,你不应该在没有必要的时候初始化,但偶尔检查所有非构造函数初始化的变量是很好的,因为它们是非常频繁且难以发现的错误的来源。

I run Cppcheck every few months. This gives me more than one hundred 'false' warnings like "Member variable 'foo::bar' is not initialized in the constructor." but once in a while it discovers some real bugs so it is totally worth it.

我每隔几个月运行一次 Cppcheck。这给了我一百多个“错误”警告,例如“成员变量 'foo::bar' 未在构造函数中初始化。” 但偶尔它会发现一些真正的错误,所以这是完全值得的。