OOPortal
J2EEOnline RationalDB
prev prev
  Course navigation
    Overloading binary operators
Using a member function to overload a binary operator
In contrast, let us overload binary minus with a member function:
class clock {
   .....
   clock  operator-(clock c);
};

clock clock::operator-(clock c)
{
   return (tot_secs - c.tot_secs);
}
Remember that there is an implicit first argument. This takes some getting used to. It would have been better to use a friend function for binary minus, because of the symmetrical treatment of both arguments.
  Course navigation