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:


 bool operator<( const String &, const String & );






C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net