Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading ==, !=, Equals

I've already gone through question

I understand that, it is necessary to implement ==, != and Equals().

public class BOX {     double height, length, breadth;      // this is first one '=='     public static bool operator== (BOX obj1, BOX obj2)     {         return (obj1.length == obj2.length                      && obj1.breadth == obj2.breadth                      && obj1.height == obj2.height);     }      // this is second one '!='     public static bool operator!= (BOX obj1, BOX obj2)     {         return !(obj1.length == obj2.length                      && obj1.breadth == obj2.breadth                      && obj1.height == obj2.height);     }      // this is third one 'Equals'     public override bool Equals(BOX obj)     {         return (length == obj.length                      && breadth == obj.breadth                      && height == obj.height);     } } 

I assume, I've written code properly to override ==,!=,Equals operators. Though, I get compilation errors as follows.

'myNameSpace.BOX.Equals(myNameSpace.BOX)' is marked as an override  but no suitable method found to override. 

So, question is - How to override above operators & get rid of this error?

like image 652
Sagar Kothari Avatar asked Aug 23 '14 11:08

Sagar Kothari


People also ask

Does overloading == also overload !=?

NO. There is no such requirement that you Must overload != If you need to overload == . However,it is a good practice that you Should overload operators related to each other.

Which function overloads the == to operator?

In Python, overloading is achieved by overriding the method which is specifically for that operator, in the user-defined class. For example, __add__(self, x) is a method reserved for overloading + operator, and __eq__(self, x) is for overloading == .

Can we overload == operator in C++?

You can redefine or overload the function of most built-in operators in C++. These operators can be overloaded globally or on a class-by-class basis. Overloaded operators are implemented as functions and can be member functions or global functions. An overloaded operator is called an operator function.

What is the correct way to overload and equality operator?

When a binary operator is overloaded the corresponding assignment operator, if any, must be explicitly overloaded. We can use the default equality operator in an overloaded implementation of the equality operator. A public or nested public reference type does not overload the equality operator.


1 Answers

As Selman22 said, you are overriding the default object.Equals method, which accepts an object obj and not a safe compile time type.

In order for that to happen, make your type implement IEquatable<Box>:

public class Box : IEquatable<Box> {     double height, length, breadth;      public static bool operator ==(Box obj1, Box obj2)     {         if (ReferenceEquals(obj1, obj2))              return true;         if (ReferenceEquals(obj1, null))              return false;         if (ReferenceEquals(obj2, null))             return false;         return obj1.Equals(obj2);     }     public static bool operator !=(Box obj1, Box obj2) => !(obj1 == obj2);     public bool Equals(Box other)     {         if (ReferenceEquals(other, null))             return false;         if (ReferenceEquals(this, other))             return true;         return height.Equals(other.height)                 && length.Equals(other.length)                 && breadth.Equals(other.breadth);     }     public override bool Equals(object obj) => Equals(obj as Box);      public override int GetHashCode()     {         unchecked         {             int hashCode = height.GetHashCode();             hashCode = (hashCode * 397) ^ length.GetHashCode();             hashCode = (hashCode * 397) ^ breadth.GetHashCode();             return hashCode;         }     } } 

Another thing to note is that you are making a floating point comparison using the equality operator and you might experience a loss of precision.

like image 180
Yuval Itzchakov Avatar answered Sep 21 '22 05:09

Yuval Itzchakov