Connecting Tech Pros Worldwide Forums | Help | Site Map

Member initialization list question

Bryan
Guest
 
Posts: n/a
#1: Jan 10 '07
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
Guest
 
Posts: n/a
#2: Jan 10 '07

re: Member initialization list question


Bryan <spam@nospam.comwrote in news:06aph.43401$wc5.41974
@newssvr25.news.prodigy.net:
Quote:
Hi,
>
Where is the proper place to use a member initialization list, the
header or cpp file?
Wherever you're defining your constructor.
Quote:
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).
Quote:
Also, is there any difference between using a member initialization
list
Quote:
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
Guest
 
Posts: n/a
#3: Jan 10 '07

re: Member initialization list question



Bryan wrote:
Quote:
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?
Quote:
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.
Quote:
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

Ron Natalie
Guest
 
Posts: n/a
#4: Jan 10 '07

re: Member initialization list question


Bryan wrote:
Quote:
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.
Quote:
>
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;
red floyd
Guest
 
Posts: n/a
#5: Jan 10 '07

re: Member initialization list question


Bryan wrote:
Quote:
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.
Quote:
>
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.
Quote:
>
vs
>
MyClass::MyClass(void)
Delete void -- It's a C-ism.
Quote:
{
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
Guest
 
Posts: n/a
#6: Jan 10 '07

re: Member initialization list question


Quote:
>
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.
Closed Thread


Similar C / C++ bytes