troubleshooting Question

constructor and member initialization

Avatar of antoinebf
antoinebf asked on
12 Comments1 Solution427 ViewsLast Modified:
I was just wondering about the best way to initialize member variables in a constructor... Scott Meyer's effective C++ recommands to init via the init list, in the same order member are declared (items 12 & 13).

... "initialization via a member initialization list is always legal, is never less efficient than assignment inside the body of the constructor, and is often more efficient."...
..."The bottom line is this: if you hope to understand what is really going on when your objects are initialized, be sure to list the members in an initialization list in the order in which those members are declared in the class."...

On the other hand, I have a friend who tells me that the compiler may order the members in any way, and that the internal representation of the class might not be that that was declared.

So my questions: is it still worth it to use the init list (I still believe it is), but is the order as important as we might think, when talking about efficiency, not possible errors (i.e. using uninit members...)?

Basically, what do you do? and why...? :)

25 points just for the fun of it!


Senior Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 12 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros