Use an overloaded helper function;
Code:
class BaseClass
{
public:
virtual bool Compare(const BaseClass *) const;
};
class DerivedClass : public BaseClass
{
public:
bool Compare(const BaseClass *) const; // override inherited virtual function
virtual bool Compare(const DerivedClass *) const; // overload function of the same name
};
bool BaseClass::Compare(const BaseClass *b) // implementation only required if BaseClass can be instantiated
{
// compare b with * this
}
bool DerivedClass::Compare(const BaseClass *b)
{
return b->Compare(this); // this is a pointer to DerivedClass, so this calls the overloaded version
}
bool DerivedClass::Compare(const DerivedClass *b)
{
// both this and b point to a DerivedClass, so ...
return details_ == b->details_;
}
This code above assumes that the pointers provided as arguments to the functions point at valid objects.