class C : public P { C++
int b;
public:
C() { ... }
bool eq(C& other) { return other.i == i && other.b == b; }
bool eq(P& other) { return other.i == i; }
};
slide: Inheritance and subtyping in C++
When we would have omitted the P variant
of eq, the compiler complains about hiding a virtual
function.
However, the same problem arises when we define eq
to be virtual in P, unless we take care to
explicitly cast p into either a C or P reference.
(Overloading is also used in [Liskov93]
to solve a similar problem.)
In the case we choose for a non-virtual definition
of eq, it is determined statically which variant
is chosen and (obviously) no problem occurs.
Considering that determining equality between
two objects is somehow orthogonal to
the functionality of the object proper,
we may perhaps better employ externally defined
overloaded functions to express relations
between objects.
This observation could be an argument to
have overloaded functions apart from
objects, not as a means
to support a hybrid approach but as a means
to characterize relations between objects
in a type consistent (polymorphic) fashion.