views:

61

answers:

2

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!

+5  A: 

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.

Georg Fritzsche
is there a difference between what the member initialization list does and what initializing variables through default constructors does, other than efficiency?
alphacentauri
@alpha: What do you mean "through the default constructor"? At this stage, you shouldn't really be worried about efficiency anyway but correctness of code.
GMan
@alpha: If you mean explicit vs. implicit default construction/initialization - there is only a difference for those types that would be left uninitialized.
Georg Fritzsche
@alphacentauri: The question make so sense. You can use initialization list in default constructor. Your question suggest that they are mutually exclusive, which is not true.
AndreyT
i was wondering or not initializing data in a default ctor has the same effect as using a member initialization list. I remember hearing somewhere that initialization and assignemt are not the same thing
alphacentauri
Do you mean *assign values* in the constructor body? You can use a member initialization list (MIL) in the constructor: `foo::foo() : MLI {}`.
GMan
@alpha: I think an example of what you mean would help.
Georg Fritzsche
A: 

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.

Peter
ok great!thank you
alphacentauri
Not true in general. Members that aren't specified will be processed (or not) in accordance with their specific rules. For example, members of type `int` will be left uninitialized. To say that they will be "default-constructed" is incorrect. You can't "construct" and `int`. It has no constructor.
AndreyT