Question

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;
}
Was it helpful?

Solution

To clarify from my comment, this will support code so you can either provide all variations of operators:

inline bool operator==(const MyClass& lhs, const MyClass& rhs) {
return lhs.middle == rhs.middle;
}

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;
}

And do that for every operator (which blows up into a significant amount of code). OR if it makes sense to, you can provide a constructor which constructs from int:

struct MyClass {
MyClass() {} // default
MyClass( int x ) { /* init for an int here */ }
int start;
int middle;
int threequarters;
};

If you do this, then you will only need the MyClass,MyClass version for each operator:

inline bool operator==(const MyClass& lhs, const MyClass& rhs) {
return lhs.middle == rhs.middle;
}

Because when the compiler sees:

if ( 5 == my_class ) {}

It actually does this:

if ( MyClass(5).operator==( my_class ) ) {}

OTHER TIPS

Yes, you need to define three operator == provided that your class has no conversion constructor for objects of type int.

inline bool operator==(const MyClass& lhs, const MyClass& rhs) {
    return lhs.middle == rhs.middle;
}

inline bool operator==(const MyClass& lhs, int rhs) {
    return lhs.middle == rhs;
}

inline bool operator==(int lhs, const MyClass& rhs) {
    return operator ==( rhs, lhs );
}

Yes, you need both operators.

bool operator==(const A& a, const int & b)
{
    return a.value == b;
};

With only the above operator defined, the compiler will throw an error that no conversion is available if you try the following: 2 == a (considering that the class A has no implicit constructors that take an integer as an argument)

For each binary operation, the compiler tries to do an implicit conversion to fit the operation. For instance, if class A had an operator int definition, you would not need the operators, since the compiler would implicitly convert the A type to an integer and then perform the bool operator==(int,int) operation. If no operator is defined, then the compiler throws an error, saying that there is no conversion available (i.e, no operator == that accepts A as an argument on either left or right side)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top