470,815 Members | 1,320 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,815 developers. It's quick & easy.

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

May 30 '06 #1
4 1574
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.
May 30 '06 #2
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.
May 30 '06 #3
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.

May 30 '06 #4
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.
May 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Sandy | last post: by
14 posts views Thread by Magius | last post: by
5 posts views Thread by Remco van Engelen | last post: by
6 posts views Thread by Simon | last post: by
5 posts views Thread by fdmfdmfdm | last post: by
reply views Thread by sovixi | last post: by
5 posts views Thread by Ryan Krauss | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.