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