I need to be able to compare one of my classes (which contains a lot more than an integer) to integers, even though that might be stretching the like of equality a little it's close enough...
How do I overload the equality operator for different types?
I basically have a class like this
struct MyClass {
int start;
int middle;
int threequarters;
};
and the overloaded operator
inline bool operator==(const MyClass& lhs, const MyClass& rhs) {
return lhs.middle == rhs.middle;
}
So when comparing with integers I need to compare against the middle variable as well, but I'm not sure if I need two sets of operator functions, one where integer is lhs and one where integer is rhs?
inline bool operator==(const int& lhs, const MyClass& rhs) {
return lhs == rhs.middle;
}
inline bool operator==(const MyClass& lhs, const int& rhs) {
return lhs.middle == rhs;
}
MyClass(int i) : middle(i) {}.bool areEqualAtMiddle(const MyClass& lhs, const MyClass& rhs)MyClassin a set and at some points during the execution I only have access to the middle value, so I need to get the reference back somehow...