Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Member initialization list question (http://www.velocityreviews.com/forums/t459757-member-initialization-list-question.html)

Bryan 01-10-2007 06:18 PM

Member initialization list question
 
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

Andre Kostur 01-10-2007 06:21 PM

Re: Member initialization list question
 
Bryan <spam@nospam.com> 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)

Gavin Deane 01-10-2007 06:30 PM

Re: Member initialization list question
 

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


red floyd 01-10-2007 06:34 PM

Re: Member initialization list question
 
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.

Ron Natalie 01-10-2007 06:54 PM

Re: Member initialization list question
 
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;

Ron Natalie 01-10-2007 07:15 PM

Re: Member initialization list question
 

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


All times are GMT. The time now is 06:54 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.