Wow, a lot of the other answers were so totally unnecessary. dynamic_cast- it exists, use it.
class Animal {
public:
virtual bool operator==(const Animal& other) = 0;
virtual ~Animal() = 0;
};
template<class T> class AnimalComp : public Animal {
public:
virtual bool operator==(const Animal& ref) const {
if (const T* self = dynamic_cast<const T*>(&ref)) {
return ((T*)this)->operator==(*self);
}
return false;
}
virtual bool operator!=(const Animal& ref) const {
if (const T* self = dynamic_cast<const T*>(&ref)) {
return ((T*)this)->operator!=(*self);
}
return true;
}
};
class Monkey : public AnimalComp<Monkey> {
public:
virtual bool operator==(const Monkey& other) const {
return false;
}
virtual bool operator!=(const Monkey& other) const {
return false;
}
};
class Snake : public AnimalComp<Snake> {
public:
virtual bool operator==(const Snake& other) const {
return false;
}
virtual bool operator!=(const Snake& other) const {
return false;
}
};
Edit: Bow before my automatic templated implementation!
Edit edit: One thing I did do was forget to tag them as const, which was wrong of me. I will not apologize for not doing != as, let's face it, implementing it is a total doddle.
More edits: Jesus Christ guys, this is not an example on how to write != or ==, it's an example of how to use the CRTP. If you don't like how I chose to implement my != or ==, you can sue.