c++ list of overloadable operators code example

Example 1: c++ operator overloading

// money.h -- define the prototype
class Money
{
   public:
      Money & operator += (const Money &rhs);
}

// money.cpp -- define the implementation
Money& Money :: operator += (const Money &rhs)
{
   // Yadda Yadda
  
   return *this;
}

Example 2: equals operator c++ overlaod

MyClass& MyClass::operator=(const MyClass &rhs) {

    // Only do assignment if RHS is a different object from this.
    if (this != &rhs) {
      ... // Deallocate, allocate new space, copy values...
    }

    return *this;
  }

Tags:

Cpp Example