Information Technology Reference
In-Depth Information
Evaluating User-Defined Conversions
The user-defined conversions discussed so far have directly converted the source type to an
object of the target type in a single step, as shown in Figure 18-26.
Figure 18-26.
Single-step user-defined conversion
But user-defined conversions can have up to three steps in the full conversion. Figure 18-27
illustrates these stages, which include
The preliminary standard conversion
The user-defined conversion
The following standard conversion
There is
never
more than a single user-defined conversion in the chain.
Figure 18-27.
Multi-step user-defined conversion
Example of a Multi-Step User-Defined Conversion
The following code declares class
Employee
, which is derived from class
Person
.
Since there is a user-defined conversion from class
Person
to
int
, then if there is a stan-
dard conversion from
Employee
to
Person
and one from
int
to
float
, you can convert
from
Employee
to
float
.
-
There is a standard conversion from
Employee
to
Person
, since
Employee
is derived
from
Person
.
There is a standard conversion from
int
to
float
, since that is an implicit numeric
conversion.
-
Since all three parts of the chain exist, you can convert from
Employee
to
float
.