about classes and initialization | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

about classes and initialization

I stumbled upon this piece of code in my c++ book (ulrich breymann c++, eine Einführung page 138, for anyone interested) and i can't really make sense of the " : name_(n), points_(0) {} "thing. Why the ":", why the"{}" is this about initializing? I am also curious if it is really necessary to use const so often. I appreciate any help of you guys. Link to the code: https://code.sololearn.com/cG13QUY7sq8N/?ref=app

22nd Oct 2018, 8:24 AM
Innocent Culprit
Innocent Culprit - avatar
1 Answer
+ 7
You've stumbled upon the constructor initialiser list. Some situations which require you to use them instead of the old fashioned initialization within the constructor body is listed in this link, the first one being non-static const member, as portrayed in the example you provided: https://www.geeksforgeeks.org/when-do-we-use-initializer-list-in-c/ As for whether or not const is needed in the context above, it really depends on the requirements of the project.
22nd Oct 2018, 9:19 AM
Hatsy Rei
Hatsy Rei - avatar