Should constructor initialize all the data members of the class?

前端 未结 5 658
暗喜
暗喜 2021-02-02 13:48

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 logi

相关标签:
5条回答
  • 2021-02-02 14:04

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

    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.

    0 讨论(0)
  • 2021-02-02 14:05

    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?

    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.

    or it is just Eclipse's logic?

    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.2 or some other coding standard or style guide.

    0 讨论(0)
  • 2021-02-02 14:10

    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.

    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.

    0 讨论(0)
  • 2021-02-02 14:11

    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.

    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.

    0 讨论(0)
  • 2021-02-02 14:25

    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

    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. Of course that is to be expected as the CDT Code Analysis' remit is greater than that of the compiler.

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

    0 讨论(0)
提交回复
热议问题