pYro_65:
Overloading can only be done by varying the argument list. The return type must be the same.
That is not quite correct.
The argument list must be different, but return types can be anything, the limitation is when using pure virtual functions, not virtual functions.
If a base member has a certain prototype, a user can expect a certain functionality in derived types, so it is perfectly valid for a common base set of overloads to return different types.
It is not 'valid' when the overload introduces uniqueness into the derived types, and a unique member should be used rather than an overloaded member.
When I said the return type has to be the same I was thinking about code where the only thing that changes is the return type of the function.
Example A:
class OverloadTest {
int sum(int a, int b) { return a+b; };
float sum(int a, int b) { return (float)a + (float)b; };
};
Example B:
class BaseClass {
virtual int sum(int a, int b) { return a+b; };
};
class DerivedClass : public OverloadTest {
virtual float sum(int a, int b) { return (float)a + (float)b; };
};
Trying to compile Example A gives:
error: ‘float OverloadTest::sum(int, int)’ cannot be overloaded with ‘int OverloadTest::sum(int, int)’
Trying to compile Example B gives:
error: conflicting return type specified for ‘virtual float DerivedClass::sum(int, int)’
Of course I was wrong when I stated the return type must be the same, because if one changes the method argument list, then the return type can also be different:
class OverloadTest {
int sum(int a, int b) { return a+b; };
float sum(float a, float b) { return (float)a + (float)b; };
};
class BaseClass {
virtual int sum(int a, int b) { return a+b; };
};
class DerivedClass : public BaseClass {
virtual float sum(float a, float b) { return (float)a + (float)b; };
};