Initializer List

T

The Directive

I'm having trouble understanding the benefits and usage of the
initializer list. How is it better than initializing class members
inside the constructor's body? Are initializer list only related to
constructors?

For example:

//counter is an int.
Count::Count()
{
counter = 0;
}

versus:

Count::Count: counter(0)
{
}

The C++ books I have don't do a good job explaining the initlizer
List. One of them only mention that "the reasons are complex but have
to do with the fact that members initialized in the initializer list
are given a value before the constructor even starts to execute. This
is important in in some situations. For example, the initializer list
is the only way to initialize const member data and references" Can
someone provide me examples that validate the last stement? I'm having
a hard time grasping this concept. Other examples that exemplified
benefits of the initializer list over normal constructor body
initialization would be very helpful.

Thanks,
--The Directive.
 
J

Jacques Labuschagne

The said:
I'm having trouble understanding the benefits and usage of the
initializer list. How is it better than initializing class members
inside the constructor's body? Are initializer list only related to
constructors?

For example:

//counter is an int.
Count::Count()
{
counter = 0;

This isn't initialisation, this is assignment. At this point, counter
has been default-initialised and you're assigning a new value to it.
}

versus:

Count::Count: counter(0)

This causes counter to be /initialised/ with value 0. In effect you're
passing 0 to counter's constructor, which the other way uses the
default constructor and then an assignment.
{
}

The C++ books I have don't do a good job explaining the initlizer
List. One of them only mention that "the reasons are complex but have
to do with the fact that members initialized in the initializer list
are given a value before the constructor even starts to execute. This
is important in in some situations. For example, the initializer list
is the only way to initialize const member data and references" Can
someone provide me examples that validate the last stement? I'm having

The point is that between the end of an initialisation list and the
start of the body of the constructor, all members that haven't been
initialised get /default/ initialised. It's just more efficient to use
the initialiser lists.

With non-static const members, as you point out, one *must* use the
lists :

struct X{
const int i;
X(): i(7){}
};

The alternative wouldn't compile, because you can't assign anything
to const objects.

HTH,
Jacques
 
J

Jerry Coffin

I'm having trouble understanding the benefits and usage of the
initializer list. How is it better than initializing class members
inside the constructor's body?

It's useful because it IS initialization, rather than assignment. The
distinction is most obvious when you're dealing with a const or a
reference, because they HAVE to be truly initialized, and can't be
assigned to later (you can force the same requirement in user-defined
objects). Just for example, this is allowed:

class X {
const int x;
int &y;
public:

X() : x(1), y(x) {}
};

but this is not:

class X {
const int x;
int &y;
public:

X() {
x = 1; // not allowed -- assigning to a const
y = x; // this means something different than we want, but
// there's really not even any syntax to mean what
// we need here.
};
Are initializer list only related to constructors?

Yes.

Given that anything of const or reference type _must_ be initialized
with an initialization list, I (for one) consider it better to do all
possible initialization in an initializer. I haven't done tried to do
real statistical analysis, but I'd guess that the majority of ctors I
write have empty bodies.

I'd also note that I generally _prefer_ to define classes so that
objects of that class _must_ be initialized. In this case, if another
object contains an intance of the class in question, it (again) must
initialize it in the initializer. The point of this is to prevent an
object from being created with a meaningless value.
 
R

Ron Natalie

Jacques Labuschagne said:
This isn't initialisation, this is assignment. At this point, counter
has been default-initialised and you're assigning a new value to it.

No actually, counter may or may be default initialized or not initialzied
at all. The stupidity of the C++ language is that POD classes do not always
get initialized.
 
J

Jacques Labuschagne

Ron said:
No actually, counter may or may be default initialized or not initialzied
at all. The stupidity of the C++ language is that POD classes do not always
get initialized.

Really? I was going by 12.6.2/3 which says
- if the /expression-list/ of the /mem-initializer/ is omitted, the
base class or member subobject is default-initialized (see 8.5).
- otherwise, the subobject indicated by /mem-initializer-id/ is
direct-initialized using /expression-list/ as the initializer
(see 8.5).

Jacques
 
R

Ron Natalie

Jacques Labuschagne said:
Really? I was going by 12.6.2/3 which says
- if the /expression-list/ of the /mem-initializer/ is omitted, the
base class or member subobject is default-initialized (see 8.5).
- otherwise, the subobject indicated by /mem-initializer-id/ is
direct-initialized using /expression-list/ as the initializer
(see 8.5).

Nope... neither case is present here. This only speaks about when the mem-intiializer
is present (either with or without the expression list between the parens). If the mem-initializer
is not present, you need to drop down to paragraph 4 which talks about members/bases
not explicitly initialized.
 
J

Jacques Labuschagne

Ron said:
Nope... neither case is present here. This only speaks about when the mem-intiializer
is present (either with or without the expression list between the parens). If the mem-initializer
is not present, you need to drop down to paragraph 4 which talks about members/bases
not explicitly initialized.

Right, thanks.

Jacques
 
J

Jeff Schwab

Ron said:
No actually, counter may or may be default initialized or not initialzied
at all. The stupidity of the C++ language is that POD classes do not always
get initialized.

It's not "stupidity."
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top