One of the nice features of cast operators and conversion constructors is that, when necessary, the compiler can call these functions implicitly to create temporary objects. For example, if an object s
of a user-defined String
class appears in a program at a location where an ordinary char *
is expected, such as
cout << s;
the compiler can call the overloaded cast-operator function operator char *
to convert the object into a char *
and use the resulting char *
in the expression. With this cast operator provided for a String
class, the stream insertion operator does not have to be overloaded to output a String
using cout
.
Software Engineering Observation 10.5
When a conversion constructor or conversion operator is used to perform an implicit conversion, C++ can apply only one implicit constructor or operator function call (i.e., a single user-defined conversion) to try to match the needs of another overloaded operator. The compiler will not satisfy an overloaded operator’s needs by performing a series of implicit, user-defined conversions.
3.141.2.34