6.5. Overloadable Operators

The C++ compiler parses expressions into operands and operators, as shown in Table 6.9.

With the exception of the ternary conditional operator (?:), all C++ operators work with either one operand (unary) or two operands (binary).

Table 6.9. Operator formats in C++
Expression Format Operator Type Examples
left_operand operator right_operand Binary a + b, a > b
left_operand operator Unary (Postfix) a++, a--, a->
operator right_operand Unary (Prefix) ++a, --a, *a
operand1 ? operand2 : operand3 Ternary a > b ? a : b

Table 6.10 lists the unary operators that you may overload.

Table 6.10. Unary operator overloading
Operator Examples Operator Examples
+ +a ~ ~a
- -a ! !a
* *a ++ a++, ++a
& &a -- a--, --a
-> a->   

Table 6.11 ...

Get Navigating C++ and Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.