C ++ Primer: Chapter 4 summary
Chapter 4: Expressions
4.1 basis
Fundamental contents:
- Unary, binary operators, ternary operator, function call (special operator).
- Precedence and associativity of the operators, the order of evaluation value calculation target.
- Operand conversion, overloaded operators, left and right values.
Remarks:
- Identity (position) values using the object's left and right value to use value of the object (content). Alternatively the value of the left and right values, not vice versa.
- Clear order of evaluation of four operators: and (&&), or (||), condition (:?), The comma (,).
4.2 Arithmetic Operators
Fundamental contents:
- One yuan plus sign (+), unary minus sign (-).
- Multiply (*), division (/), take the remainder (%).
- Plus (+), minus (-).
Remarks:
- Arithmetic operators satisfy left associativity, and its evaluation result operands are the right values.
- Boolean values should not be involved in operations.
- The arithmetic expression may produce undefined results because the situation such as divide by zero or overflow.
- Will remove the remainder of the division, the value 0 always supplier; modulo operands must be integers, m% (- n) is equivalent to m% n, (- m)% n and (-m)% (- n) It is equivalent to - (m% n).
4.3 logical and relational operators
Fundamental contents:
- Logical not (!).
- Less than (<), less than or equal to (<=), greater than (>), less than (> =).
- Equal (=), not equal (! =).
- Logic (&&).
- Logical OR (||).
Remarks:
- In addition to logic NOT (!) Is right-associative, other logical and relational operators are left-associative. Operand and evaluates all logical and relational operators are the right values.
- Logic (&&) and logical OR (||) (only the right side of the object is calculated not only when the object left Release Results) using the short-circuit evaluation.
- Types reference avoid excessive copying large elements.
- When the comparison operation, unless the comparison is a Boolean type, do not use true and false.
4.4 Assignment Operators
Fundamental contents:
- Assignment (=).
- In line with the assignment operator (+ =, - =, * =, / =,% =, << =, >> =, & =, ^ =, | =)
Remarks:
- Right assignment operator satisfies the associative law, the result is the left side of the assignment operator operand, which is a left-modifiable value.
- initialization! = Assignment. If the left operand is built-in type, the assignment initialization list contain up to a value, but may be a plurality of initialization. The int i1 [3] = {1, 2, 3}; right, int i2 [3]; i2 = {1, 2, 3}; is wrong.
- Compound operator is equivalent to a = a op b;
4.5 increment and decrement operators
Fundamental contents:
- Increment (++), decrement (-).
- The object itself as the front-left version value returned, post-release copy of the original value of the object as the value of the right to return.
Remarks:
- Unless necessary, do not use the post version. Because post-release the original value to be stored as a return value, the greater the compiler overhead.
- Post-increment (decrement) operator precedence> dereferencing operator. * P ++ is equivalent to * (p ++).
4.6 member access operator
Fundamental contents:
- Point operator, the arrow operator (.) (->).
- ptr-> men equivalent (* ptr) .men.
- Priority: dot operator, the arrow operator> increment decrement operators> dereferencing operator.
4.7 conditional operator
Fundamental contents:
- The conditional operator (? :)
- Conditional operator form: cond? exp1: exp2. If cond is true is executed exp1, otherwise execution exp2.
- Conditional operation nesting level is preferably not more than two to three.
4.8 Operators
Fundamental contents:
- Bit negated (-).
- Left (<<), right (>>).
- Bit and (&).
- Bitwise XOR (^).
- Bit or (|)
Remarks:
- Bit operand operator is an integer, and for setting the check bit.
- Bitwise operators try just for processing an unsigned integer with the sign bit signed integer no clear processing method dependent on the particular machine.
- Shift operator (<< and >>) satisfies the associative law left and right operands can not be negative, and the result is less than the number of bits.
4.9 sizeof operator
Fundamental contents:
- sizeof operator form: sizeof (type) or sizeof expr.
- Right sizeof satisfies the associative law, and the same operator precedence dereferencing.
4.10 comma operator
Fundamental contents:
- Comma operator first calculation expression on the left side, the result is discarded, and then calculation expression on the right, return the result.
4.11 Type Conversion
Fundamental contents:
- Implicit conversion: arithmetic conversions, integral promotion unsigned type conversion, array to a pointer, between the pointer conversion, converted into Boolean type, is converted into the constants, class type definitions conversion.
- 显式转换:static_cast, dynamic_cast, const_cast, reinterpret_cast。
Remarks:
- Avoid cast.