I have a base class
class Animal
with pure virtual functions, and a set of derived classes
class Monkey : public Animal
class Snake : public Animal
I want to implement a comparison operation so that, if I encounter two pointers to Animals in my code
Animal* animal1
Animal* animal2
I can compare them to each other. The comparison should yield false, if animal1 and animal2 are of different derived classes. If they are of the same derived class, the output of the comparison operator should be returned.
Can someone point me to a good way of implementing this?
Wow, a lot of the other answers were so totally unnecessary. dynamic_cast- it exists, use it.
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: this is not an example on how to write != or ==, it’s an example of how to use the CRTP.