Conversion Functions

You can define a member function of a class, called a conversion function, that converts from the type of its class to another specified type.

The conversion function specifies a conversion from the class type the conversion function is a member of, to the type specified by the name of the conversion function . Classes, enumerations, and typedef names cannot be declared or defined as part of the function name.

Conversion functions have no arguments, and the return type is implicitly the conversion type. Conversion functions can be inherited. You can have virtual conversion functions but not static ones.

Only one user-defined conversion is implicitly applied to a single value. User-defined conversions must be unambiguous, or they are not called.

If a conversion function is declared with the keyword const , the keyword has no affect on the function except for acting as a tie- breaker when there is more than one conversion function that could be applied. Specifically, if more than one conversion function could be applied, all of these functions are compared. If any of these functions is declared with the keyword const, const is ignored for the purposes of this comparison. If one of these functions is a best match, this function is applied. If there is no best match, the functions are compared again, but this time const is not ignored.



Example of a Conversion Function


Conversion by Constructor
Virtual Functions
Syntax of a Conversion Function