How do I get an overloaded relational operator in a class to be called from a function in the parent class that is handed a const reference to a base class as a parameter? The following code demonstrates what I would like to do:
class Object
{
public:
virtual ~Object(void);
virtual int compare(Object const& obj) const;
};
int Object::compare(Object const & obj) const {
if(this == &obj)
{
return 0;
}
else if(this < &obj)
{
return -1;
} else{
return 1;
}
}
class Integer: public Object
{
private:
int myInt;
public:
Integer(int i);
bool operator==(const Integer& integer);
};
bool Integer::operator==(Integer const &integer) {
if(myInt == integer.myInt)
{
return true;
}
return false;
}
How do I get the compare function in the base class to call the == operator in the child class, keeping in mind I have other child classes as well?
I have tried dynamic_cast<> but for some reason it wont work.