(float)
of
(float)23
is an unary operator. As with other operators, cast
operator can be overloaded as well.
There is really not much to say about cast operators, except that their declaration is a little different. Let's use the following example:
class X { int i,j; public: // ... operator float (void); };
The only member function is operator float (void)
. Unlike other
operators, which require a specification of the return type, cast operators
have their return type implied by the operator itself. As a result, the
cast operator to convert to float
has an implied return type of
float
. The (void)
part is just to specify the operator
has no parameters.
The definition of this operator may look like the following:
X::operator float(void) { return (float)i/j; }
The use of the operator is illustrated by the following example:
{ X var1; printf("%f\n", (float)var1); }