www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 581 (continued)]

11.7. Overloading Binary Operators

A binary operator can be overloaded as a non-static member function with one argument or as a global function with two arguments (one of those arguments must be either a class object or a reference to a class object).

Later in this chapter, we will overload < to compare two String objects. When overloading binary operator < as a non-static member function of a String class with one argument, if y and z are String-class objects, then y < z is treated as if y.operator<( z ) had been written, invoking the operator< member function declared below

      class String

      public:
         bool operator<( const String & ) const;
         ...
      }; // end class String

If binary operator < is to be overloaded as a global function, it must take two argumentsone of which must be a class object or a reference to a class object. If y and z are String-class objects or references to String-class objects, then y < z is treated as if the call operator<( y, z ) had been written in the program, invoking global-function operator< declared as follows:


[Page 582]
      bool operator<( const String &, const String & );


Previous Page
Next Page