question about constructor grammar..



 DEVELOP > c-Plus-Plus > question about constructor grammar..

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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.
.





  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER