Binary Operators


The latest version of this topic can be found at Binary Operators.

The following table shows a list of operators that can be overloaded.

Redefinable Binary Operators

&Bitwise AND
&&Logical AND
&=Bitwise AND/assignment
–>Member selection
–>*Pointer-to-member selection
<Less than
<<Left shift
<<=Left shift/assignment
<=Less than or equal to
>Greater than
>=Greater than or equal to
>>Right shift
>>=Right shift/assignment
^Exclusive OR
^=Exclusive OR/assignment
|Bitwise inclusive OR
&#124;=Bitwise inclusive OR/assignment
&#124;&#124;Logical OR

To declare a binary operator function as a nonstatic member, you must declare it in the form:

ret-type operatorop( arg )

where ret-type is the return type, op is one of the operators listed in the preceding table, and arg is an argument of any type.

To declare a binary operator function as a global function, you must declare it in the form:

ret-type operatorop( arg1, arg2 )

where ret-type and op are as described for member operator functions and arg1 and arg2 are arguments. At least one of the arguments must be of class type.

System_CAPS_ICON_note.jpg Note

There is no restriction on the return types of the binary operators; however, most user-defined binary operators return either a class type or a reference to a class type.

Operator Overloading