Can conversion functions be non-member functions

Is it possible to define a casting operator from one type to another type outside the class definition as a function that is not a member? I know that this is possible for other operators, such as an operator, but this is not possible with translation operators. For example, for two classes A and B, I tried to define a casting operator outside areas A and B as follows:

operator A(const B& b)
{
    A a(....);
    return a;
}
+4
source share
2 answers

No, conversion functions must be member functions.

From C ++ 11, [class.conv.fct] / 1:

A - X, [operator conversion-type-id], X , . .

, , , .

+6

i.e . , , : -

: -

class Rational
{
  public:
     operator double ();
};

double Rational double.

0

All Articles