C++ Operators Bob Walton, 7/16/96 The name `operator ==' can be used like a function name: x == y <=====> operator == ( x, y ) This is just a matter of surface syntax. But it allows us to define operators. For example: enum color { RED, GREEN, ANY }; bool operator == ( color x, color y ) { if ( int ( x ) == int ( ANY ) ) return true; else if ( int ( y ) == int ( ANY ) ) return true; else return int ( x ) == int ( y ); } The operator == above implements the rewrite equations: x == ANY ===> true ANY == y ===> true x == y ===> int( x ) == int( y ) if the previous cases do not apply Then: RED == GREEN ===> false RED == RED ===> true RED == ANY ===> true ANY == GREEN ===> true operator == ( RED, ANY ) ===> RED == ANY ===> true C++ does NOT allow you to redefine an existing operator definition. E.g. foo * operator + ( foo * p, int i ) { ... } is ILLEGAL as p+i is predefined. In fact, C++ requires any operator you define to have at least one argument that is a class (or struct or union) or enum. Existing versions of some C++ compilers (g++) require operators to have a class or struct argument in order to work correctly.