1.6 Operators

Operators specify an evaluation to be performed on one of the following:

One operand (unary operator)

Two operands (binary operator)

Three operands (ternary operator)

The C++ language includes all C operators and adds several new operators. The following syntax lists those operators unique to C++ first, and it then lists the operators shared between C and C++.

Syntax

operator: one of

C++ Operators .* : : delete
  –>* new,  

C/C++ Operators [ % *=
  ] << /=
  ( >> %=
  ) < +=
  . > –=
  –> <= <<=
  ++ >= >>=
  –– == &=
  & != ^=
  *   |=
  + | '
  – && #
  ~ || ##
  ! ? :> (Microsoft C and C++ specific)
  sizeof :,  
  / =,  

Operators follow a strict precedence. This precedence defines the evaluation order of expressions containing these operators. Operators associate with either the expression on their left or the expression on their right; this is called “associativity.” Table 1.2 shows the precedence and associativity of C++ operators (from highest to lowest precedence).

Table 1.2 C++ Operator PrecedenceSyntax, and Associativity

Operator
Name or Meaning
Syntax
Associativity
     

== Equality equality-expression == relational-expression Left to right
    ,  
:: Scope resolution class-name :: name, None  
:: Global :: name None

[ ] Array subscript postfix-expression [ expressionopt ] Left to right
( ) Function call postfix-expression ( expression-listopt ) Left to right
( ) Conversion simple-type-name ( expression-listopt ) None
. Member selection (object) postfix-expression . name Left to right
–> Member selection (pointer) postfix-expression –> name Left to right
++ Postfix increment postfix-expression ++ None
–– Postfix decrement postfix-expression ––, None  

new Allocate object ::opt new placementopt new-type-name new-initializeropt None
    ::opt new placementopt ( type-name ) new-initializeropt None

delete Deallocate object ::opt delete cast-expression, None  
delete[ ]   ::opt delete [ ] cast-expression, None  

++ Prefix increment ++ unary-expression None
–– Prefix decrement –– unary-expression, None  
* Dereference * cast-expression None
& Address-of & cast-expression None
+ Unary plus + cast-expression None
– Arithmetic negation (unary) – cast-expression, None  

Table 1.2 (continued)

Operator
Name or Meaning
Syntax
Associativity
     

! Logical NOT ! cast-expression None
~ Bitwise complement ~ cast-expression None
:> Base operator base-expression :> expression, None  

sizeof Size of object sizeof unary-expression, None  
sizeof ( ) Size of type sizeof( type-name ), None  

(type) Type cast (conversion) ( type-name ) cast-expression Right to left

.* Apply pointer to class member (objects) pm-expression .* cast-expression Left to right
–>* Dereference pointer to class member pm-expression –>* cast-expression Left to right

* Multiplication multiplicative-expression * pm-expression Left to right
/ Division multiplicative-expression / pm-expression Left to right
% Remainder (modulus) multiplicative-expression % pm-expression Left to right

+ Addition additive-expression + multiplicative-expression Left to right
– Subtraction additive-expression – multiplicative-expression Left to right

<< Left shift shift-expression << additive-expression Left to right
>> Right shift shift-expression >> additive-expression Left to right

< Less than relational-expression < shift-expression Left to right
> Greater than relational-expression > shift-expression Left to right

Table 1.2 (continued)

Operator
Name or Meaning
Syntax
Associativity
     

<= Less than or equal to relational-expression <= shift-expression Left to right
>= Greater than or equal to relational-expression >= shift-expression Left to right
!= Inequality equality-expression != relational-expression Left to right

& Bitwise AND and-expression & equality-expression Left to right

^ Bitwise exclusive OR exclusive-or-expression ^ and-expression Left to right

| Bitwise OR inclusive-or-expression | exclusive-or-expression Left to right

&& Logical AND logical-and-expression && inclusive-or-expression Left to right

|| Logical OR logical-or-expression || logical-and-expression Left to right

e1?e2:e3 Conditional logical-or-expression ? expression : conditional-expression Right to left

= Assignment unary-expression = assignment-expression Right to left
*= Multiplication assignment unary-expression *= assignment-expression Right to left
/= Division assignment unary-expression /= assignment-expression Right to left
%= Modulus assignment unary-expression %= assignment-expression Right to left
+= Addition assignment unary-expression += assignment-expression Right to left
–= Subtraction assignment unary-expression –= assignment-expression Right to left
<<= Left-shift assignment unary-expression <<= assignment-expression Right to left

Table 1.2 (continued)

Operator
Name or Meaning
Syntax
Associativity
     

>>= Right-shift assignment unary-expression >>= assignment-expression Right to left
&= Bitwise AND assignment unary-expression &= assignment-expression Right to left
|= Bitwise inclusive OR assignment unary-expression |= assignment-expression Right to left
^= Bitwise exclusive OR assignment unary-expression ^= assignment-expression Right to left
, Comma expression, assignment-expression Left to right

The [ ], ( ), and ? : operators (array subscript, function call, and conditional, respectively) can be used only as pairs. However, these operators can be separated by expressions (see Chapter 4, “Expressions,” for more information).

The # and ## operators can occur only in #define preprocessor directives.