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