TOP STORIES
PM to face MPs' fury after ruling Evening StandardUS House moves to impeach Trump The Washington PostBREXIT CHAOS
Rees-Mogg accuses court of a 'constitutional coup' Daily MailJohnson quotes Greek myth to talk Brexit at UN MirrorTRENDING
Cuba 'sonic attack' mystery deepens Yahoo! News UKNEWS
Rouhani invited to discuss jailed Brits Sky NewsIMPEACHMENT INQUIRY
'Witch Hunt garbage': Trump reacts to impeachment probe The Boston GlobeAll you need to know about whistle-blower scandal IntelligencerENTERTAINMENT
McCartney: I still dream of Lennon Cover MediaFleabag star signs £16m deal The TelegraphSPORT
Guardiola defends Silva's 'racist' post The IndependentSPOTLIGHT
How much is a whale worth? National GeographicOpinion: The growing threat to journalism around world The New York TimesROYALS IN AFRICA
Harry 'feeling overwhelmed' during visit to mosque PA MediaHow Meghan & Harry are breaking royal protocol CosmopolitanTALKING POINTS
French chef sues Michelin guide The iDON'T MISS
UK's top hotels for a minibreak MirrorIs corn a fruit or vegetable? Popular ScienceMOST READ
Dewan 'overjoyed' at pregnancy news Harper's BazaarBenefits of vitamin E supplement US News & World Report - Health
Active5 years, 6 months ago
The other two members, qty and p, can be initialized differently in each of three different constructors, so they are initialized inside the constructors' initializer lists. A curiosum: Note that providing an inline initializer prevents your class from having a trivial default constructor.
Is it good practice when writing C++11 code to set default values for class members in the header file of the class?
Or is it better to do this in the constructor of the class?
EDIT:
I mean:
foo.h:
VS
foo.cpp (of course with the necessary header file, but without the in-class initialization):
Which one is better and why?
Paul
PaulPaul12.3k55 gold badges4040 silver badges6161 bronze badges
3 Answers
If a class member is always initialized with the same initial value, then you should make the initializer inline, so as to avoid duplication. If the initial value depends on the constructor, then put it in the constructor initializer list. (And never use assignment in the way you did.)
Example:
In this hypothetical example, the member
done
is always required to start as false
, so it's best to write the initializer inline. The other two members, qty
and p
, can be initialized differently in each of three different constructors, so they are initialized inside the constructors' initializer lists.A curiosum: Note that providing an inline initializer prevents your class from having a trivial default constructor.
Kerrek SBKerrek SB
382k6767 gold badges731731 silver badges967967 bronze badges
It depends whether you need to stay compatible with older C++ compilers .When you are not using C++11 you have to initialize most members (all non-static) in the constructor.Further many people advocate to explicitly initialize every member even if this means explicitly calling the default ctor.Usually you should place implementation details in a cpp file not in the header file, thus an example would be
In C++11 you have more choices and in class member initializer will become very handy, especially if you have several cors. Here is a good link for more information: http://www.stroustrup.com/C++11FAQ.html#member-init
After Edit: According to your code you are using C++11. To my knowledge there is only few information on good practice concerning the new possibilities but IMHO In class member initializer are very handy to concentrate initialization in one place, which reduces complexity and typing
Bulletmagnet2,82311 gold badge1717 silver badges3737 bronze badges
MartinMartin2,54111 gold badge2222 silver badges4747 bronze badges
Initializing in headers has the main advantages of keeping code more local and easy to understand. It saves also some typing.
The main disadvantage, in my opinion, is the need to include more headers to get access to constructors. Simple forward declaration won't suffice, making compilation take longer.
Christopher OezbekChristopher Oezbek11.9k33 gold badges3737 silver badges6161 bronze badges