Overloading comparison operators for different types in C ++

I need to be able to compare one of my classes (which contains a lot more than an integer) with integers, although this may stretch this equality a bit, but it is close enough ...

How to overload the equality operator for different types?

I basically have this class

struct MyClass { int start; int middle; int threequarters; }; 

and overloaded operator

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

Therefore, when comparing with integers, I also need to compare the average variable, but I'm not sure if I need two sets of operator functions, where integer is lhs and 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; } 
+6
source share
3 answers

To clarify from my comment, this will support the code so that you can either provide all the options for the 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 it for each operator (which will explode on a significant amount of code). OR, if that makes sense, you can provide a constructor that builds from int:

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

If you do this, you only need the version of MyClass, MyClass for each statement:

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

Because when the compiler sees:

 if ( 5 == my_class ) {} 

Actually it does:

 if ( MyClass(5).operator==( my_class ) ) {} 
+4
source

Yes, you need to define three == operators, provided that your class does not have a 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 ); } 
+1
source

Yes, you need both operators.

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

Only with the above statement does the compiler throw an error if no conversion is available if you try the following: 2 == a (given that class A has no implicit constructors that take an integer as an argument)

For each binary operation, the compiler attempts to perform an implicit conversion corresponding to the operation. For example, if class A has an operator int definition, you won’t need operators, since the compiler will implicitly convert type A to an integer, and then perform the bool operator==(int,int) operation. If the operator is not defined, the compiler throws an error stating that there are no conversions available (i.e.No operator == that takes A as an argument on the left or right side)

0
source

All Articles