Why do implicit conversion member functions overloading work by return type, while it is not allowed for normal functions?
c++, implicit-conversion, polymorphism
Solution
Conversion operators are not really considered different overloads and they are not called based on their return type. The compiler will only use them when it has to (when the type is incompatible and should be converted) or when explicitly asked to use one of them with a cast operator.
Semantically, what your code is doing is to declare several different type conversion operators and not overloads of a single operator.
Problem
C++ does not allow polymorphism for methods based on their return type. However, when overloading an implicit conversion member function this seems possible. Does anyone know why? I thought operators are handled like methods internally. Edit: Here's an example: ``` struct func { operator string() { return "1";} operator int() { return 2; } }; int main( ) { int x = func(); // calls int version string y = func(); // calls string version double d = func(); // calls int version cout << func() << endl; // calls int version } ```