- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 567字
- 2021-06-10 18:27:52
Using Operators
Operators are used to compute a value from one or more operands. The following table groups all of the operators with equal precedence and lists their associativity. The higher in the table, the higher precedence of execution the operator has in an expression. If you have several operators in an expression, the compiler will perform the higher-precedence operators before the lower-precedence operators. If an expression contains operators of equal precedence, then the compiler will use the associativity to decide whether an operand is grouped with the operator to its left or right.
There are some ambiguities in this table. A pair of parentheses can mean a function call or a cast and in the table these are listed as function() and cast(); in your code you will simply use (). The + and - symbols are either used to indicate sign (unary plus and unary minus, given in the table as +x and -x), or addition and subtraction (given in the table as + and -). The & symbol means either "take the address of" (listed in the table as &x) or bitwise AND (listed in the table as &). Finally, the postfix increment and decrement operators (listed in the table as x++ and x--) have a higher precedence than the prefix equivalents (listed as ++x and --x).
For example, take a look at the following code:
int a = b + c * d;
This is interpreted as the multiplication being performed first, and then the addition. A clearer way to write the same code is:
int a = b + (c * d);
The reason is that * has a higher precedence than + so that the multiplication is carried out first, and then the addition is performed:
int a = b + c + d;
In this case, the + operators have the same precedence, which is higher than the precedence of assignment. Since + has left to right associativity the statement is interpreted as follows:
int a = ((b + c) + d);
That is, the first action is the addition of b and c, and the result is added to d and it is this result that is used to assign a. This may not seem important, but bear in mind that the addition could be between function calls (a function call has a higher precedence than +):
int a = b() + c() + d();
This means that the three functions are called in the order b, c, d, and then their return values are summed according to the left-to-right associativity. This may be important because d may depend on global data altered by the other two functions.
It makes your code more readable and easier to understand if you explicitly specify the precedence by grouping expressions with parentheses. Writing b + (c * d) makes it immediately clear which expression is executed first, whereas b + c * d means you have to know the precedence of each operator.
The built-in operators are overloaded, that is, the same syntax is used regardless of which built-in type is used for the operands. The operands must be the same type; if different types are used, the compiler will perform some default conversions, but in other cases (in particular, when operating on types of different sizes), you will have to perform a cast to indicate explicitly what you mean.