Member initialization list question

B

Bryan

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
 
A

Andre Kostur

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)
 
G

Gavin Deane

Bryan said:
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-lite/ctors.html#faq-10.6

Gavin Deane
 
R

red floyd

Bryan said:
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.
 
R

Ron Natalie

Bryan said:
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;
 
R

Ron Natalie

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top