I'm trying to do something like this:
class foo {
virtual void bool operator==(foo const & rhs) = 0;
};
class bar1 : public foo {
bool operator==(bar1 const & rhs) { ... }
};
class bar2 : public foo {
bool operator==(bar2 const & rhs) { ... }
};
That is, I want to indicate that all classes implementing the foo
interface must implement the operator==
method for its own derived class.
But, the compiler is complaining that bar1 and bar2 are still abstract classes because they haven't implemented operator==(foo const &)
yet.
I've considered changing the function signature to foo const &
in both bar1 and bar2 then doing dynamic_cast
inside the function, but that seems messy:
class bar1 : public foo {
bool operator==(foo const & rhs) {
const bar1 * casted_rhs = dynamic_cast<const bar1 *>(&rhs);
if (casted_rhs == NULL) {
// not a bar1
return false;
} else {
// go through rhs and this object and find out if they're equal
}
}
}
This feels messy.
There must be a better way to do this.