Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 706
alphacentauri Avatar asked Mar 07 '26 00:03

alphacentauri


2 Answers

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.

like image 127
Georg Fritzsche Avatar answered Mar 08 '26 14:03

Georg Fritzsche


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.

like image 40
Rich Hammond Avatar answered Mar 08 '26 14:03

Rich Hammond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!