Why member initialization?

This is a discussion on Why member initialization? within the C++ Programming forums, part of the General Programming Boards category; I don't want to be too much of an annoying newb on all of you 1337 h4x0rz But I have ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    267

    Post Why member initialization?

    I don't want to be too much of an annoying newb on all of you 1337 h4x0rz But I have some notes from class, and on the back bottom page there is a little "helpful note" but it was neither explained nor used...

    I have talked with fellow classmates about this and have gotten answers, but I am the type of guy who likes to see what a wide variety of people say so I can make better overall judgments... SO.... can you please help explain the following....... (in your own words)

    Code:
    //
    // DO THIS - Member initialization
    //
    User::User( const RefParam &inParam )
    : mPointerMember( new PointerMember( inParam ))
    {
    	return;
    }
    
    //
    // DO NOT DO THIS
    //
    User::User( const RefParam &inParam )
    {
    	mPointerMember = new PointerMember( inParam );
    	return;
    }
    So like I said.... you're own explainations would be quite appreciated
    Last edited by d00b; 06-12-2002 at 10:26 PM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I dont think there is a big difference in this case. It's not like the PointerMember is getting constructed and then re-assigned, so the two aren't really different, from my knowledge, unlike they would be if PointerMember wasn't a pointer, but instead a reference (it would be illegal), or a value (it would be constructed to default and then assigned).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    114
    Theres acctually a catch when doing it the first way, I think its something like the variables must be in the order thay are declared or something. get screwed up otherwise. Might be some old school stuff but youll never know...

    I really don't think you need to care about that nowadays bcause the compiler would fix that up the best way possible anyway... the final result would be the same anyhow... i say choose the way that you think looks better...

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Well actually... after speaking to others, like I said I would so... I concluded that it's a matter of effeciency...

    In the second constructor, mPointerMember is "assigned" using the "=" operator, which means in the copy constructor, a temporary value has to be created which takes up memory. Also it is run-time ineffecient because there is an extra call to the class constructor and destructor for that temporarily created value

    In the first constructor, mPointerMember is assigned in the mem-initializer list, which avoids all that

    I think......

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    The only thing I know about this is as follows: You are forced to use the first method in the case of const member variables. They are impossible to initialize any other way. As for anything else, do what feels best for you. Using the initializers lists probably looks cooler but if its going to confuse you or someone else responsible for debugging your code, then go for the second method. Mostly, I think the more experienced programmers will tend to use the first method while newbies will go for the second.

    Code:
    class MyClass{
        const int iValue;
    ...
    ...
    ...
    };
    
    MyClass::MyClass( const int val ) : iValue( val ) // Works!!!
    {
    }
    
    MyClass::MyClass( const int val )
    {
        iValue = val;   // Won't work!!!
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Cool, I didn't know that... thanks for the heads up (we'll be doing this in class tomorrow)

  7. #7
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    But in the case of a pointer member, it's only constructing a pointer, not a value object. An extra assignment of a pointer is not really going to do much in terms of run time cost.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    Yea, my friend pointed that out too... well, that's why I'm a student. Learning something every day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21