简体   繁体   English

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

[英]Should constructor initialize all the data members of the class?

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 m in the constructor. 在构造函数中初始化m的应用程序逻辑中没有任何意义。 However, Eclipse warns me that the constructor leaves m uninitialized. 然而,Eclipse的警告我说,构造叶m未初始化。 I can't run the code somewhere else now. 我现在无法在其他地方运行代码。 The warning is: 警告是:

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的逻辑?

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. 它不是c ++标准所要求的。 As long as you initialize all variables before they're used, your program is correct in that regard. 只要您在使用之前初始化所有变量,您的程序在这方面是正确的。

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. 当所有警告都启用时,我测试的g ++和clang版本都没有警告。 The logic may or might not be based on high integrity c++ coding standard 12.4.2 or some other coding standard or style guide. 该逻辑可能或可能不是基于高完整性c ++编码标准12.4.2或一些其他编码标准或样式指南。

For completeness, the warning comes from the C/C++ Code Analysis. 为完整起见,警告来自C / C ++代码分析。 In particular the problem is Potential Programming Problems / Class members should be properly initialized 特别是问题是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. 您可以禁用整个警告,或仅在违反警告条件的文件上禁用它。

显示警告

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. 至于将CDT与GCC或CLang进行比较,这似乎是CDT与编译器提供的内容相比进行额外代码分析的情况。 Of course that is to be expected as the CDT Code Analysis' remit is greater than that of the compiler. 当然,这是预期的,因为CDT代码分析的范围大于编译器的范围。

PS, If you are up for it, you can read the implementation of this particular checker . PS,如果您愿意,可以阅读此特定检查器的实现。

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 permanently be in a consistent state, which include both public (accessible) attributes and private attributes as well. 最后,每个对象应永久处于一致状态,包括公共(可访问)属性和私有属性。 Optimization should not be a reason for keeping an object un-consistent. 优化不应成为保持对象不一致的原因。

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. 这就是为什么C / C ++永远不会将内置类型初始化为成员或自动变量的原因 - 因为这样做会妨碍性能。 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. 当然,在创建一次对象/变量时这不是问题(这就是静态初始化静态的原因),但是对于在紧密循环中发生的事情,默认初始化可能会耗费宝贵的纳秒。

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初始化的,因为具有无效指针是未定义行为的直接方式。

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. 我每隔几个月就会跑一次Cppcheck。 This gives me more than one hundred 'false' warnings like "Member variable 'foo::bar' is not initialized in the constructor." 这给了我超过一百个'false'警告,例如“成员变量'foo :: bar'未在构造函数中初始化。” but once in a while it discovers some real bugs so it is totally worth it. 但有一段时间它会发现一些真正的错误,所以它是完全值得的。

暂无
暂无

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

相关问题 consteval 构造函数必须初始化所有数据成员吗? - Must consteval constructor initialize all data members? 默认构造函数是否始终初始化所有成员? - Does a default constructor always initialize all members? 如果明确定义构造函数,是否必须初始化类成员? - Is it mandatory to initialize class members if a constructor is explicitly defined? 在C ++中,当我想在初始化类对象数组时通过构造函数初始化类成员时,我该怎么办? - In C++, What should I do when I want to initialize class members via constructor when I initialize a class object array? 类作为其他类的成员:创建一个接受所有数据的构造函数 - Class as members of other classes : Create a constructor that takes all data 如何在其构造函数中初始化其所有成员的类在C ++ 11中初始化 - How a class that does not initialize its all members in its constructor to be initialized in C++11 在C ++中,如果我们没有在构造函数中初始化它们,那么数据成员的值在类中将是什么? - in C++, what would the the value of data-members be in class if we did not initialize them in constructor? 将类的静态const数据成员初始化为一个类 - Initialize static const data members of a class into a class 用错误初始化构造函数中的成员 - Initialize members in constructor with error 如何从子类的构造函数初始化父类的私有成员 - How to initialize private members of parent class from subclass's constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM