web analytics

Overloading Operator != Correspondingly When Operator == Get Overloaded in C++

Options

codeling 1599 - 6654
@2016-01-07 13:23:47

If used correctly, operator overloading can improve the readability of the code. This is the case for classes that represent mathematical quantities such as complex numbers and for classes that replace arrays or pointers.

C++ programmers expect that all operators in a set of closely related operators are available.

For example, if a class provides == for comparing two objects of the class, it should also provide != .

In general, many relationships between operators can be described as a set of invariants.

For example, if a and b are int s and if a != b is true, this implies that !(a == b) is also true. The same property should hold if a and b are objects of a class.

The general recommendation is that if you overload operators, provide all operators in a closely related set of operators and preserve the invariants that are valid for built-in types.

If a class provides copy assignment and operator==() , two objects are expected to be equal after assigning one of them to the other.

Int x = 42;
Int y = 0;
x = y;
// x == y should be true

If a class provides the comparison operators, < , <= , > and >= , we expect that an object can either be lesser, greater or equal to another object. For example, if we have a function max that returns the largest of two operands, it should not matter what operator is used in the implementation.

Int max(Int x, Int y)
{
   if (x > y) // could use: < instead
   {
      // We also expect that:
      // y < x
      return x;
   }
   else
   {
      // We also expect that:
      // x <= y
      return y;
   }
}

It can be useful to preserve an invariant by using an operator member function in the implementation of another closely related operator member function. You could say the invariant is the implementation, since it defines how to implement an operator function in terms of another overloaded operator function.

The following MyString class overloads operator ==() and operator !=() . The implementation of operator!=() compares two strings and returns true if they are not equal.

class MyString
{
   public:
      MyString(const char* cp);
      size_t length() const;
      .......
      bool operator==(const MyString& s) const;
      bool operator!=(const MyString& s) const;
      ......
   private:
      size_t len;  // Length of string
      char*  cp;      // A pointer to the characters
};
bool MyString::operator!=(const MyString& s) const
{
   if (len != s.len)  
   // Different lengths means that strings are different
   {
      return true;
   }
   else
   {
      return (strcmp(cp, s.cp) != 0);
   }
}

To check if two strings are equal, we can simply negate the result of operator !=() . By doing that, less code is needed to implement operator ==() .

bool MyString::operator==(const MyString& s) const
{
   return !(*this != s);  // operator!= used here
}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com