C++ Unary Operators

The unary operators are shown in Table 12.4.

Table 12.4   Redefinable Unary Operators

Operator Name
! Logical NOT
& Address-of
~ One’s complement
* Pointer dereference
+ Unary plus
++ Increment
– Unary negation
–– Decrement

Of the operators shown in Table 12.4, the postfix increment and decrement operators (++ and ––) are treated separately in Increment and Decrement.

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

ret-type operatorop()

where ret-type is the return type and op is one of the operators listed in Table 12.4.

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

ret-type operatorop( arg )

where ret-type and op are as described for member operator functions and the arg is an argument of class type on which to operate.

Note   There is no restriction on the return types of the unary operators. For example, it makes sense for logical NOT (!) to return an integral value, but this is not enforced.