CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2008
    Posts
    61

    An Object Oriented Question about C++

    I don't know this is in the right section or not, then if it's not in the right section please move the topic to the right location .
    I have been seen a snippet which has the following
    Code:
    CCallbackHandler::CCallbackHandler():
    	m_hModPsapi(NULL),
    	m_pfnEnumProcessModules(NULL),
    	m_pfnGetModuleFileNameEx(NULL)
    {
        /// rest of code ...
    }
    I don't know which of object orientation aspects used in this source code but I'm confused that what is this :
    Code:
    CCallbackHandler::CCallbackHandler():
    	m_hModPsapi(NULL),
    	m_pfnEnumProcessModules(NULL),
    	m_pfnGetModuleFileNameEx(NULL)
    what's this technique is called?

    ty.
    Last edited by escap3; March 31st, 2010 at 06:05 AM.

  2. #2
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: An Object Oriented Question about C++

    Thats called constructor initialization list.
    This will ensure that all the data member are initialized before you hit constructor body.

    If you dont do that, all the data mambers will be initialized anyway with default, before reaching constructor body.
    Dont forget to rate my post if you find it useful.

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: An Object Oriented Question about C++

    Yes. Some people argue that these should be always used, but the choice is yours, after you learn more about the topic, of course.

    A quick web search came up with some articles that you might find helpful:
    http://www.custard.org/~andrew/optimize.php ("Use Initialization Lists" section)
    http://www.goingware.com/tips/parame...embervars.html ("The Initialization List" section)
    http://publib.boulder.ibm.com/infoce...ef/cplr388.htm (all)


    EDIT:

    P.S. BTW, this doesn't have much to do with the OO concepts - i would rather call this a language mechanism.
    Last edited by TheGreatCthulhu; March 31st, 2010 at 06:49 AM. Reason: The P.S.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: An Object Oriented Question about C++

    Quote Originally Posted by TheGreatCthulhu
    http://www.goingware.com/tips/parame...embervars.html ("The Initialization List" section)
    A comment on this article's statement:
    Note that it is terribly important that you initialize pointer members (actually any member) of your objects in the constructor's initialization list.
    I agree, except that in a more complex case, I would initialise the pointer member to be a null pointer, and then use new[] in the constructor body. Of course, it may be possible to avoid such a more complex case by moving the pointer into a separate class for the purposes of RAII. See GotW #66: Constructor Failures.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2008
    Posts
    61

    Re: An Object Oriented Question about C++

    Thanks all, I'm going to read more about initialization lists .

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: An Object Oriented Question about C++

    Initializer lists are nice in that they may provide a slight efficiency benefit. The only time they're absolutely required is when a class member does not have a default constructor; in this case, you must pass that member a parameter in the initializer list, or else you'll get a compile error.

  7. #7
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: An Object Oriented Question about C++

    Quote Originally Posted by Lindley View Post
    Initializer lists are nice in that they may provide a slight efficiency benefit. The only time they're absolutely required is when a class member does not have a default constructor; in this case, you must pass that member a parameter in the initializer list, or else you'll get a compile error.
    In addition to this, class members that are references must be initialised in the initialiser list.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: An Object Oriented Question about C++

    Quote Originally Posted by PredicateNormative View Post
    In addition to this, class members that are references must be initialised in the initialiser list.
    and if the class has const members, then they can only be initialized in the initializer list too (references are kind like const pointers (not to be confused with pointer to const)).

    Well, actually they don't have to be initialized in the initializer list, but you can't give them a value anywhere else.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: An Object Oriented Question about C++

    As long as we're getting specific, the above statements only apply to non-static members in all three cases.

  10. #10
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: An Object Oriented Question about C++

    Quote Originally Posted by Lindley View Post
    As long as we're getting specific, the above statements only apply to non-static members in all three cases.
    This is great fun ... We could also include base classes that do not have default constructors.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured