Click here to Skip to main content
15,908,111 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: member variable initialization Pin
toxcct18-Dec-07 3:52
toxcct18-Dec-07 3:52 
GeneralRe: member variable initialization Pin
CPallini18-Dec-07 4:12
mveCPallini18-Dec-07 4:12 
QuestionRe: member variable initialization Pin
David Crow18-Dec-07 4:13
David Crow18-Dec-07 4:13 
JokeRe: member variable initialization Pin
CPallini18-Dec-07 4:18
mveCPallini18-Dec-07 4:18 
GeneralRe: member variable initialization Pin
George_George18-Dec-07 18:24
George_George18-Dec-07 18:24 
GeneralRe: member variable initialization Pin
George L. Jackson18-Dec-07 4:00
George L. Jackson18-Dec-07 4:00 
GeneralRe: member variable initialization Pin
George_George18-Dec-07 18:26
George_George18-Dec-07 18:26 
GeneralRe: member variable initialization Pin
George L. Jackson19-Dec-07 2:48
George L. Jackson19-Dec-07 2:48 
Yes, it does impact the way you write a class.

In addition to that, the rules for guaranteed object initialization to take place (By the way, I consider POD types objects too!) are complicated. Thus, I always initialize my objects before I use them. In a class, this is done in the initialization list in the order the member variables appear in the class. Using the initialization list is the most efficient technique. Avoid assignment whenever possible! Please note, however, that, with built-in types (POD types), there is no extra cost in using assignment over initialization but having consistant code is always a plus.

Our example code demonstrated the classes A and B were initialize with or without an initialization list in the order of appearance in class C. Placing theses classes in class C's initialization list might seem like overkill to you. However, by placing them in the initialization list, you erase any doubt about whether you classes have been properly initialized.

Finally, order of initialization due to placement of member variables can get a little tricky if a member variable requires other member variables to be initialized before its initialization. Thus, you have to order your member variables accordingly. If you cannot order your member variables due to a really complicated initialization dependency, you may have to uses other methods such as assignment in the constructor body.

I hope I was clear,
George

References:
[Meyers2005] Scott Meyers,Effective C++: 55 Specific Ways to Improve your Programs and Designs, Third edition, Addison-Wesley.
"We make a living by what we get, we make a life by what we give." --Winston Churchill

GeneralRe: member variable initialization Pin
George_George19-Dec-07 2:59
George_George19-Dec-07 2:59 
GeneralRe: member variable initialization Pin
toxcct18-Dec-07 3:13
toxcct18-Dec-07 3:13 
GeneralRe: member variable initialization Pin
George L. Jackson18-Dec-07 4:05
George L. Jackson18-Dec-07 4:05 
GeneralRe: member variable initialization Pin
toxcct18-Dec-07 4:10
toxcct18-Dec-07 4:10 
GeneralRe: member variable initialization Pin
George L. Jackson18-Dec-07 4:34
George L. Jackson18-Dec-07 4:34 
GeneralRe: member variable initialization Pin
Nelek18-Dec-07 23:37
protectorNelek18-Dec-07 23:37 
GeneralRe: member variable initialization Pin
eraccn18-Dec-07 4:43
eraccn18-Dec-07 4:43 
QuestionHow to make the font bold in the header of a ListControl Pin
Vikas_12318-Dec-07 2:04
Vikas_12318-Dec-07 2:04 
Generalnamespace problem Pin
prithaa18-Dec-07 1:59
prithaa18-Dec-07 1:59 
QuestionRe: namespace problem Pin
CPallini18-Dec-07 3:40
mveCPallini18-Dec-07 3:40 
GeneralRe: namespace problem Pin
jhwurmbach18-Dec-07 4:33
jhwurmbach18-Dec-07 4:33 
GeneralRe: namespace problem Pin
Member 75496018-Dec-07 8:11
Member 75496018-Dec-07 8:11 
QuestionHow to create a Unique numeric ID Pin
SWDevil18-Dec-07 1:52
SWDevil18-Dec-07 1:52 
AnswerRe: How to create a Unique numeric ID Pin
CPallini18-Dec-07 2:14
mveCPallini18-Dec-07 2:14 
GeneralRe: How to create a Unique numeric ID Pin
SWDevil18-Dec-07 2:17
SWDevil18-Dec-07 2:17 
GeneralRe: How to create a Unique numeric ID Pin
SWDevil18-Dec-07 2:23
SWDevil18-Dec-07 2:23 
GeneralRe: How to create a Unique numeric ID Pin
CPallini18-Dec-07 2:33
mveCPallini18-Dec-07 2:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.