Constructor Initializer Lists: When (and why) Should I use Them?

Hey guys!
I was doing some examples from my tutorial, and found that after introducing constructor initializer lists, it almost always used them over initializing in the body of the constructor, but I never found out why. When should I use them, and what benefit do they give?
Last edited on
Simply, it is better because it simply constructs the member rather than default constructing it before you assign to them in the constructor body. If the member has no default constructor or you want to call a specific constructor, you must use the initializer list to do that.
Thanks for the reply, Zhuge. Are there any cases where I shouldn't use the initializer lists?
https://isocpp.org/wiki/faq/ctors#init-lists
http://yosefk.com/c++fqa/fqa.html#fqa-10.6

Also what you mean is "initialization list".

"initializer list" means something else since C++11 (the C++ standard of year 2011).

http://www.stroustrup.com/C++11FAQ.html#uniform-init
http://www.stroustrup.com/C++11FAQ.html#init-list
Last edited on
thanks!
Topic archived. No new replies allowed.