C++ Operators
C++ operators are the math-like symbols that allow our programs to do different things with our variables, like add them together or print them out to the screen. The list of C++ operators appears in Appendix 2, pg 1001, in your text. That list also appears below.
Operators have two characteristics which we have to keep in mind when we use them. These are (1) precedence and (2) associativity. Precedence tells use which operators goes first if there are more than one of them. For example, in the equation x = 4 + 5 * 2;, x gets the value 14, because the multiplication is done first (because it has higher precedence, see the table below).
When two operators have the same precedence, then the one that goes first is according to the operators associativity. For example, in the equation x = 4 + 5 - 6; the 4 and 5 are first added, then the 6 is subtracted, since the precedence for addition and subtraction are the same we look at the associativity and see that the addition on the left should be done first.
| Operator | Description | Association | Precedence |
| () | function call | left | 17 |
| [] | subscript | left | |
| . -> | selection, indirect selection | left | |
| :: | scope resolution | left | |
| ++ -- (postfix version) | increment, decrement | right | 16 |
| ! ~ | logical not, bitwise not | right | 15 |
| + - | arithmetic plus, negation | right | |
| ++ -- (prefix version) | increment, decrement | right | |
| & * | address, indirection | right | |
| sizeof | size | right | |
| () | cast | right | |
| new delete | allocation, free | right | |
| . * | member selection | left | 14 |
| -> * | member pointer selection | left | |
| * / % | multiplicative | left | 13 |
| + - | additive | left | 12 |
| << >> | insertion, extraction, shift | left | 11 |
| < <= > >= | comparison | left | 10 |
| == != | equality | left | 9 |
| & | bitwise and | left | 8 |
| ^ | bitwise xor | left | 7 |
| | | bitwise or | left | 6 |
| && | logical and | left | 5 |
| || | logical or | right | 4 |
| ? : | conditional | right | 3 |
| = *= /= += -= etc. | assignment, cmpd. assign. | right | 2 |
| , | sequential evaluation | left | 1 |
In the lost above there may be a few operators which are not familiar. I've list a few of these below that we are going to be using. Therefore, you should read up on what they do. You'll find this information online here (read down to the information on the OR operator, i.e. ||).