Should constructor initialize all the data members of the class?

前端 未结 5 657
暗喜
暗喜 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: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.

提交回复
热议问题