c++ member initialization list completeness
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!
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.
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.
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.
精彩评论