Initialization Lists?

Started by
2 comments, last by Hodgman 14 years, 10 months ago
If you all will excuse the possibly noobish question... While looking for some info on a rather tangential topic, I cam across this page: clicky So I now know what an Initialization List is. I played around with it a little and it seems to make sense as an alternate way of doing things. What I am curious about is this. Firstly, why am I halfway through a Masters program and have some experience therein with c++, yet I have never heard of an initialization list before this day? Is that purely a failing of my program? Are initialization lists as important as the author of the aforelinked page advocates? To paraphrase, he seems to be saying that ALL initializations should be handled in initialization lists. Is this overdoing it? Is there some drawback to using them or is it as simply as it seems (namely that it does the same thing as initializing in the body of the constructor but with a few added advantages)? Anyway, I'm mostly just curious and most of my searches here turned up simply how to use an initialization list and nothing more... :)

I'm working on a game! It's called "Spellbook Tactics". I'd love it if you checked it out, offered some feedback, etc. I am very excited about my progress thus far and confident about future progress as well!

http://infinityelephant.wordpress.com

Advertisement
To answer your first question, yes, your program is failing. It's unfortunate that so many comp sci students come out of school bright eyed and eager only to find out that they just spent four years learning about a lot of things but having had no experience applying any of it in a practical way.

Nuff said about that.

As for initialization lists, it's quickly becoming an unwritten standard and code that doesn't make use of initialization lists is often considered sub-par. Initialization lists are a very fast way of initializing internal member variables to safe states and also helps to ensure that derived classes initialize their members properly.

I haven't looked at the article you linked to but there are a number of articles that go into very good detail about exactly why you should use init lists and why not doing so is a good way of introducing bugs into your program.

Hope this helps!

-Lead developer for OutpostHD

http://www.lairworks.com

Quote:Is there some drawback to using them or is it as simply as it seems (namely that it does the same thing as initializing in the body of the constructor but with a few added advantages)?


Therein lies the difference. Anything you do in the body of the ctor is not initialization, but assignment. All of an object's members must be constructed before control enters the body of the ctor, so if you don't use an initialization list the members will be initialized with their default constructor. Initialization lists should be the preferred method unless you have a specific need to delay assignment until the body of the ctor.
Quote:Original post by Plethora
What I am curious about is this. Firstly, why am I halfway through a Masters program and have some experience therein with c++, yet I have never heard of an initialization list before this day? Is that purely a failing of my program?
In my experience, you can't become an expert programmer just from doing a course. C++ is such a big language that no course ever covers everything you'll need to know about it.
Just make sure that you don't fall into the trap of thinking that you've learned everything and you'll be fine ;)
Quote:Are initialization lists as important as the author of the aforelinked page advocates? To paraphrase, he seems to be saying that ALL initializations should be handled in initialization lists. Is this overdoing it? Is there some drawback to using them or is it as simply as it seems
For primitive types, it doesn't make any difference (as default initialisation is free), but with complex members (e.g. classes), then it's much better to use the initialisation list.

This topic is closed to new replies.

Advertisement