Your path is not perfect. Consider the following class derived from A :
class AA : public A { public: AA(int x, int y) : A(x), y_(y) {} private: virtual bool equals(const Base& other) const; int y_; }; bool AA::equals(const Base& other) const { const AA* pAA = dynamic_cast<const AA*>(&other); if(!pAA) return false; return A::equals(other) && y_ == pAA->y_; }
then your operatator == violates the basic rule, this is not a symmetric relation:
A a(1); AA aa(1,1); assert(a == aa); assert(!(aa == a));
A short fix will be to use typeid :
bool operator==(const Base& lhs, const Base& rhs) { return typeid(lhs) == typeid(rhs) && lhs.equals(rhs); }
source share