Reputation: 31
must class member initialization lists in c++ be complete? or can they simply initialize one or two of the member data in a class?
thanks in advance!
Upvotes: 3
Views: 410
Reputation: 11
int
does have a constructor, see Stroustrup The C++ Programming Language section 6.2.8
int j = int();
This initializes j to 0 (The value of an explicit use of the constructor for a built-in type is 0 converted to that type, thus int() is another way of writing 0. Default constructors for built-in types are important so that templates can invoke default constructors without worry, even for built in types.
Upvotes: 1
Reputation: 99092
They don't have to be complete. You can leave out base classes and non-POD class types that are default constructible, POD-types however will be left uninitialized.
Obviously constant members and references have to be initialized in the member initialization list.
Upvotes: 5
Reputation: 7335
No, they don't have to be complete - any members which aren't specified in it will be default-constructed (this includes any base classes).
Obviously, any members which aren't default-constructible must be explicitly initialised. And a small gotcha - types like integers or floats etc will not be initialised, so their initial value will be undefined.
Upvotes: 0