Operator overloading.

An operator function is a function whose name consists of the keyword operator followed by a unary or binary operator #, in the form

operator #

The following operators cannot be overloaded:

Except for the operator new, operator delete, and operator ->, operator functions can return any data type. The precedence, grouping, and number of operands cannot be changed. Except for the () operator, operator functions cannot have default arguments.

An operator function must be either a non-static member function or a non-member function having at least one parameter whose type is a class, reference to a class, enumeration, or reference to an enumeration. (Thus, the operators as defined for intrinsic data types cannot be redefined.)

Binary and unary operators

A binary operator function can be defined by either a non-static member function taking one argument or a nonmember function taking two arguments. For any binary operator #, aa#bb can be interpreted as either aa.operator#(bb) or operator#(aa,bb).

A unary operator, whether prefix or postfix, can be defined by either a non-static member function taking no arguments or a nonmember function taking one argument.

 

Example: unary operators.

class C1{

public:

C1 & operator ++(); //prefix

C1 & operator --(); //prefix

C1 & operator ++(int); //postfix

C1 & operator --(int); //postfix

};