Skip to content

Latest commit

 

History

History
83 lines (64 loc) · 2.68 KB

File metadata and controls

83 lines (64 loc) · 2.68 KB
title Overloading Unary Operators | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-language
ms.tgt_pltfrm
ms.topic language-reference
dev_langs
C++
helpviewer_keywords
unary operators, plus
increment operators, overloaded
unary operators, minus
operators [C++], unary
redefinable unary operators
unary operators
pointer dereference operator overloading
plus operator
ms.assetid 7683ef08-42a4-4283-928f-d3dd4f3ab4c0
caps.latest.revision 10
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Overloading Unary Operators

The unary operators that can be overloaded are the following:

  1. ! (logical NOT)

  2. & (address-of)

  3. ~ (one's complement)

  4. * (pointer dereference)

  5. + (unary plus)

  6. - (unary negation)

  7. ++ (increment)

  8. -- (decrement)

  9. conversion operators

The postfix increment and decrement operators (++ and --) are treated separately in Increment and Decrement.

Conversion operators are also discussed in a separate topic; see User-Defined Type Conversions.

The following rules are true of all other unary operators. To declare a unary operator function as a nonstatic member, you must declare it in the form:

ret-type operator op ()

where ret-type is the return type and op is one of the operators listed in the preceding table.

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

ret-type operator op (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.

See Also

Operator Overloading