Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Member initialization list question

Reply
Thread Tools

Member initialization list question

 
 
Bryan
Guest
Posts: n/a
 
      01-10-2007
Hi,

Where is the proper place to use a member initialization list, the
header or cpp file?

Does it make any difference?

Also, is there any difference between using a member initialization list
and initializaing member variables in the constructor?

i.e.

MyClass(void) : a(1), b(2) {};

vs

MyClass::MyClass(void)
{
a = 1;
b = 2;
}

Thanks B
 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      01-10-2007
Bryan <> wrote in news:06aph.43401$wc5.41974
@newssvr25.news.prodigy.net:

> Hi,
>
> Where is the proper place to use a member initialization list, the
> header or cpp file?


Wherever you're defining your constructor.

> Does it make any difference?


Yes, probably. But that's only because member bodies which are defined
directly within the definition of a class are implicitly marked as
inlined functions (reminder: inline is only a hint to the compiler, the
compiler is not required to respect the hint).

> Also, is there any difference between using a member initialization

list
> and initializaing member variables in the constructor?
>
> i.e.
>
> MyClass(void) : a(1), b(2) {};
>
> vs
>
> MyClass::MyClass(void)
> {
> a = 1;
> b = 2;
> }


Yes. See the FAQ, section 10.6 (http://www.parashift.com/c++-faq-
lite/ctors.html#faq-10.6)
 
Reply With Quote
 
 
 
 
Gavin Deane
Guest
Posts: n/a
 
      01-10-2007

Bryan wrote:
> Hi,
>
> Where is the proper place to use a member initialization list, the
> header or cpp file?


The initialisation list is part of the definition of the constructor.
If the constructor definition is in the header, that is where the
initialisation list will be. If the constructor definition is in the
implementation file, that is where the initialisation list will be. Do
you usually put your constructor definitions in the header or the
implementation file? Or does it depend?

> Does it make any difference?
>
> Also, is there any difference between using a member initialization list
> and initializaing member variables in the constructor?


Using an initialisation list is the only way you can initialise members
in a constructor.

> i.e.
>
> MyClass(void) : a(1), b(2) {};
>
> vs
>
> MyClass::MyClass(void)
> {
> a = 1;
> b = 2;
> }


The difference is that the latter is NOT initialisation. It is
assignment.
http://www.parashift.com/c++-faq-lit....html#faq-10.6

Gavin Deane

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      01-10-2007
Bryan wrote:
> Hi,
>
> Where is the proper place to use a member initialization list, the
> header or cpp file?
>

The language syntax requires the init list to go with the definition of
the constructor.

>
> Also, is there any difference between using a member initialization list
> and initializaing member variables in the constructor?
>
> i.e.
>
> MyClass(void) : a(1), b(2) {};

Delete the semicolon. Delete void -- it's a C-ism.
>
> vs
>
> MyClass::MyClass(void)

Delete void -- It's a C-ism.
> {
> a = 1;
> b = 2;
> }
>


Yes. the first case directly constructs a and b. The second case
default constructs a and b, and then assigns them. If members are
expensive to construct and assign, you may see a performance hit (but
only worry about that *AFTER BENCHMARKING*), and the second case will
also fail if either a or b is a const member.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-10-2007
Bryan wrote:
> Hi,
>
> Where is the proper place to use a member initialization list, the
> header or cpp file?
>
> Does it make any difference?
>
> Also, is there any difference between using a member initialization list
> and initializaing member variables in the constructor?
>
> i.e.
>
> MyClass(void) : a(1), b(2) {};


This initializes the variables a and b.
>
> vs
>
> MyClass::MyClass(void)
> {
> a = 1;
> b = 2;
> }
>

This assigns values to a and b after they are (unfortunately in this
case) not initialized.

It's entirely analogous to:

int a(1);
and
int a;
a = 1;
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-10-2007

>
> Yes. the first case directly constructs a and b. The second case
> default constructs a and b, and then assigns them.


Unfortunately, it doesn't default construct a and b. It doesn't
even default initialize them in many cases.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
error C2614: 'CYYYRegister' : illegal member initialization:'CRequest' is not a base or member Angus C++ 1 03-06-2008 11:38 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57