Operator overloading ==, !=, Equals

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)) … Read more

Overload operators as member function or non-member (friend) function?

I’d go with “C++ Coding Standards: 101 Rules, Guidelines, and Best Practices”: if you can do it as non-member function, do it as non-member function (in the same namespace). One of the reasons: it works better with implicit type conversion. An Example: You have a complex class with an overloaded operator*. If you want to … Read more

Why is “operator void” not invoked with cast syntax?

The technical reason why is found in ยง12.3.2: A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it), to a (possibly cv-qualified) base class of that type (or a reference to it), or to (possibly cv-qualified) void. The rationale is (likely) … Read more

tech