21
votes

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. However, Eclipse warns me that the constructor leaves m uninitialized. I can't run the code somewhere else now. The warning is:

Member 'm' was not initialized in this constructor

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

5
I would call it correct object-oriented practice: Every variable should be initialized and the object ready for use. "No meaning"? Then initializing it to 0, -1, or MIN_INTEGER won't be harmful.duffymo
Exactly @duffymo. I am just curious if there is something in the standard that urges me to do so.gsamaras
Using the value of an uninitialised variable is undefined behaviour. The safest approach is to initialise every variable at the point of construction. For class members, your constructors should ensure that every variable is initialised or that it has a default constructor of its own that does the same.Andrew
@duffymo I thought in OO (not that anything in the question talks about OO) it shouldn't matter what a private member is set to. It is up to the class to maintain whatever invariants need to be maintained.juanchopanza
The standard doesn't say anything about it explicitly. But reading from an uninitialized variable is UB.juanchopanza

5 Answers

18
votes

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.

8
votes

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.

5
votes

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.

5
votes

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.

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.

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

0
votes

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.