Udostępnij za pośrednictwem


Operator Precedence and Associativity

The C++ language includes all C operators and adds several new 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)

Operators follow a strict precedence, which 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." The following table shows the precedence and associativity of C++ operators (from highest to lowest precedence). Operators in the same segment of the table have equal precedence and are evaluated in the given order in an expression unless explicitly forced by parentheses.

C++ Operator Precedence and Associativity

Operator

Name or Meaning

Associativity

::

Scope resolution

None

 

 

 

.

Member selection (object)

Left to right

–>

Member selection (pointer)

Left to right

[ ]

Array subscript

Left to right

( )

Function call member initialization

Left to right

++

Postfix increment

Left to right

––

Postfix decrement

Left to right

typeid( )

type name

Left to right

const_cast

Type cast (conversion)

Left to right

dynamic_cast

Type cast (conversion)

Left to right

reinterpret_cast

Type cast (conversion)

Left to right

static_cast

Type cast (conversion)

Left to right

 

 

 

sizeof

Size of object or type

Right to left

++

Prefix increment

Right to left

––

Prefix decrement

Right to left

~

One's complement

Right to left

!

Logical not

Right to left

Unary minus

Right to left

+

Unary plus

Right to left

&

Address-of

Right to left

*

Indirection

Right to left

new

Create object

Right to left

delete

Destroy object

Right to left

( )

Cast

Right to left

 

 

 

.*

Pointer-to-member (objects)

Left to right

–>*

Pointer-to-member (pointers)

Left to right

 

 

 

*

Multiplication

Left to right

/

Division

Left to right

%

Modulus

Left to right

 

 

 

+

Addition

Left to right

Subtraction

Left to right

 

 

 

<<

Left shift

Left to right

>>

Right shift

Left to right

 

 

 

<

Less than

Left to right

>

Greater than

Left to right

<=

Less than or equal to

Left to right

>=

Greater than or equal to

Left to right

 

 

 

==

Equality

Left to right

!=

Inequality

Left to right

 

 

 

&

Bitwise AND

Left to right

 

 

 

^

Bitwise exclusive OR

Left to right

 

 

 

|

Bitwise inclusive OR

Left to right

 

 

 

&&

Logical AND

Left to right

 

 

 

||

Logical OR

Left to right

 

 

 

expr1 ? expr2 : expr3

Conditional

Right to left

 

 

 

=

Assignment

Right to left

*=

Multiplication assignment

Right to left

/=

Division assignment

Right to left

%=

Modulus assignment

Right to left

+=

Addition assignment

Right to left

–=

Subtraction assignment

Right to left

<<=

Left-shift assignment

Right to left

>>=

Right-shift assignment

Right to left

&=

Bitwise AND assignment

Right to left

|=

Bitwise inclusive OR assignment

Right to left

^=

Bitwise exclusive OR assignment

Right to left

 

 

 

throw expr

throw expression

Right to left

 

 

 

,

Comma

Left to right

See Also

Reference

C++ Operators

Operator Overloading

Change History

Date

History

Reason

October 2008

Used empty rows to restore table segments. Corrected styles.

Customer feedback.