newbie: initialization lists

P

Pete

I'm trying to understand why "not using initialization lists wastes time".
Here's an excerpt from the book I'm reading:


=================== start snippet =========================================

CVehicle::CVehicle(
CPoint position,
Direction direct,
CIndicator fuel
)
{
// Body of the constructor
mCurPos = position;
mDirect = direct;
mFuelGauge = CIndicator(fuel);
cout << "Constructed the vehicle.\n";
}

Here's where the inefficiency arises. First, each of the subobjects is
constructed with their default constructor. Then the last step in the
process is the execution of the CVehicle body. Here, the data members of
the previously constructed objects are now assigned values.

=================== end snippet =========================================


I'm not sure if I fully understand this. Here's my paraphase:

1. When you declare a CVehicle, before anything else, a CPoint, Direction,
and CIndicator object is default constructed in the order they're declared
in the .h file.

2. Then, the previously declared CPoint, Direction, and CIndicator objects
which are passed to the constructor are used to re-assign the objects
which were default constructed in step 1.

Compare this to:

CVehicle::CVehicle(
CPoint position,
Direction direct,
CIndicator fuel
)
: mCurPos(position), mDirect(direct), mFuelGauge(fuel)
CIndicator fuel
)
{
// Body of the constructor is now empty.
cout << "Constructed the vehicle.\n";
}


In this case...

1. A previously constructed CPoint, Direction, and CIndicator are directly
assigned to the data members of the CVehicle as the CVehicle is in the
process of being constructed.
2. The constructor has nothing to do.


In other words, when we declare a CVehicle without initialization lists, the
members which are classes are default constructed before they are assigned
to. Using initialization lists prevents the class member objects from being
default constructed.

Thus not using initialization lists with classes that are composed of other
classes wastes time.



Is this almost correct?


Thanks!
Pete
 
V

Victor Bazarov

Pete said:
I'm trying to understand why "not using initialization lists wastes time".
Here's an excerpt from the book I'm reading:
[..]
CVehicle::CVehicle(
CPoint position,
Direction direct,
CIndicator fuel
)
> [..]
2. Then, the previously declared CPoint, Direction, and CIndicator objects
which are passed to the constructor are used to re-assign the objects
which were default constructed in step 1.

Actually, you have another source of inefficiency in your example: the
objects are passed by value, so in the item 2 you should say "the copies
of objects passed to the constructor are used".
Compare this to:
[...]

Is this almost correct?

It is correct. It is also covered in the FAQ. Also, consider passing
objects by a const reference instead of by value.

V
 
K

Karl Heinz Buchegger

Pete said:
Is this almost correct?

I would say: Yes you got it.

The difference is:

*******************************

CVehicle::CVehicle(
CPoint position,
Direction direct,
CIndicator fuel
)
{
// Body of the constructor
mCurPos = position;
mDirect = direct;
mFuelGauge = CIndicator(fuel);
cout << "Constructed the vehicle.\n";
}

Here the default constructors for position, direct and fuel are called
during the process of creating an CVehicle object. I don't know what
those default constructors do, but if it was a good programmer, then
position will initialize to 0/0, direct to something sensible and fuel
will be set to something sensible.
Then comes the body of the CVehicle constructor and changes all of those
members again by means of assignments.

********************************

CVehicle::CVehicle(
CPoint position,
Direction direct,
CIndicator fuel
)
: mCurPos(position), mDirect(direct), mFuelGauge(fuel)
CIndicator fuel
)
{
// Body of the constructor is now empty.
cout << "Constructed the vehicle.\n";
}

Here the whole thing runs different. When the CVehicle constructor gets
called, the copy constructors for position, direct and fuel are used to
construct those members with the values passed to them.

**********************
So all in all, the whole thing is a 2 step process in the first case:
* initialize everything to defaults
* then assign with the passed values

while it is a 1 step process in the second case:
* initialize everything to the passed values
 
G

Greg Comeau

I'm trying to understand why "not using initialization lists wastes time".
Here's an excerpt from the book I'm reading:
....
Thus not using initialization lists with classes that are composed of other
classes wastes time.

Is this almost correct?

I read it quickly and it seems to be. Have a look at
http://www.comeaucomputing.com/techtalk/#meminit and not the
inserted call to sc() in the member initializer list.
Your case is similar. That means it may be running some
default ctor, and then in some case ignoring the possible
work that the default ctor did, just to assign it in a different
way. Obviously, that will take more time than just doing
it one way in most 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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top