Binary Overloaded Operators as Member Functions

Consider using < to compare two objects of a String class that you define. When overloading binary operator < as a non-static member function of a String class, 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 with one argument declared below:

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

Overloaded operator functions for binary operators can be member functions only when the left operand is an object of the class in which the function is a member.

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.