-
March 31st, 2010, 06:02 AM
#1
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.
-
March 31st, 2010, 06:20 AM
#2
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.
-
March 31st, 2010, 06:44 AM
#3
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.
-
March 31st, 2010, 06:57 AM
#4
Re: An Object Oriented Question about C++
Originally Posted by TheGreatCthulhu
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.
-
March 31st, 2010, 07:24 AM
#5
Re: An Object Oriented Question about C++
Thanks all, I'm going to read more about initialization lists .
-
March 31st, 2010, 09:54 AM
#6
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.
-
March 31st, 2010, 11:03 AM
#7
Re: An Object Oriented Question about C++
Originally Posted by Lindley
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.
-
March 31st, 2010, 11:17 AM
#8
Re: An Object Oriented Question about C++
Originally Posted by PredicateNormative
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.
-
March 31st, 2010, 11:30 AM
#9
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.
-
March 31st, 2010, 12:16 PM
#10
Re: An Object Oriented Question about C++
Originally Posted by Lindley
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|