| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"kleinstein" |
| Date: |
30 May 2006 02:37:06 AM |
| Object: |
question about constructor grammar.. |
i'm newbie of C++ Language..
What i habe seen is this..
MemoryFile( char * in_name = NULL )
: name(NULL), data(NULL), size(0), position(0) {
// <-----------HIER
if( in_name != NULL ) // Copy the input name.
name = in_name;
}
this is a Constructor of some Class..
my Question is....
what is this ---> " : name(NULL), data(NULL), size(0), position(0) "
is this another Input Arguments ??
normaly i have seen always this form of Constructors..
NameOfClass(Input Arguments){
Some Initialization....
}
thanks to read..
.
|
|
| User: "Ian Collins" |
|
| Title: Re: question about constructor grammar.. |
30 May 2006 03:26:11 AM |
|
|
kleinstein wrote:
i'm newbie of C++ Language..
What i habe seen is this..
MemoryFile( char * in_name = NULL )
: name(NULL), data(NULL), size(0), position(0) {
// <-----------HIER
if( in_name != NULL ) // Copy the input name.
name = in_name;
This is silly, it should be replaced with name(in_name) in the
initialiser list.
}
this is a Constructor of some Class..
my Question is....
what is this ---> " : name(NULL), data(NULL), size(0), position(0) "
is this another Input Arguments ??
It is an initialiser list, look it up in your C++ book. These are
preferred because they enable the compiler to generate more efficient
code and are more concise than individual assignments in the constructor
body..
--
Ian Collins.
.
|
|
|
| User: "Andre Kostur" |
|
| Title: Re: question about constructor grammar.. |
30 May 2006 07:50:07 AM |
|
|
On Tue, 30 May 2006 20:26:11 +1200, Ian Collins wrote:
kleinstein wrote:
i'm newbie of C++ Language..
What i habe seen is this..
MemoryFile( char * in_name = NULL )
: name(NULL), data(NULL), size(0), position(0) {
// <-----------HIER
if( in_name != NULL ) // Copy the input name.
name = in_name;
This is silly, it should be replaced with name(in_name) in the initialiser
list.
Depends on what type name is. If it's a std::string, no it shouldn't.
You can't initialize a std::string with NULL.
.
|
|
|
| User: "Markus Schoder" |
|
| Title: Re: question about constructor grammar.. |
30 May 2006 08:51:19 AM |
|
|
Andre Kostur wrote:
On Tue, 30 May 2006 20:26:11 +1200, Ian Collins wrote:
kleinstein wrote:
i'm newbie of C++ Language..
What i habe seen is this..
MemoryFile( char * in_name = NULL )
: name(NULL), data(NULL), size(0), position(0) {
// <-----------HIER
if( in_name != NULL ) // Copy the input name.
name = in_name;
This is silly, it should be replaced with name(in_name) in the initialiser
list.
Depends on what type name is. If it's a std::string, no it shouldn't.
You can't initialize a std::string with NULL.
Then the original code would be broken as well as it does name(NULL) in
the initialiser list.
It could be a somewhat odd class however that handles constructing with
NULL gracefully but not assigning NULL.
.
|
|
|
| User: "Andre Kostur" |
|
| Title: Re: question about constructor grammar.. |
30 May 2006 09:41:28 AM |
|
|
On Tue, 30 May 2006 06:51:19 -0700, Markus Schoder wrote:
Andre Kostur wrote:
On Tue, 30 May 2006 20:26:11 +1200, Ian Collins wrote:
kleinstein wrote:
i'm newbie of C++ Language..
What i habe seen is this..
MemoryFile( char * in_name = NULL )
: name(NULL), data(NULL), size(0), position(0) {
// <-----------HIER
if( in_name != NULL ) // Copy the input name.
name = in_name;
This is silly, it should be replaced with name(in_name) in the
initialiser list.
Depends on what type name is. If it's a std::string, no it shouldn't.
You can't initialize a std::string with NULL.
Then the original code would be broken as well as it does name(NULL) in
the initialiser list.
It could be a somewhat odd class however that handles constructing with
NULL gracefully but not assigning NULL.
Yikes! You're right. Missed the initialization of name in the initializer
list.
.
|
|
|
|
|
|
|

|
Related Articles |
|
|