C ++ Primer: Chapter 4 summary


Chapter 4: Expressions

4.1 basis

Fundamental contents:

  1. Unary, binary operators, ternary operator, function call (special operator).
  2. Precedence and associativity of the operators, the order of evaluation value calculation target.
  3. Operand conversion, overloaded operators, left and right values.

Remarks:

  1. 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.
  2. Clear order of evaluation of four operators: and (&&), or (||), condition (:?), The comma (,).

4.2 Arithmetic Operators

Fundamental contents:

  1. One yuan plus sign (+), unary minus sign (-).
  2. Multiply (*), division (/), take the remainder (%).
  3. Plus (+), minus (-).

Remarks:

  1. Arithmetic operators satisfy left associativity, and its evaluation result operands are the right values.
  2. Boolean values ​​should not be involved in operations.
  3. The arithmetic expression may produce undefined results because the situation such as divide by zero or overflow.
  4. 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:

  1. Logical not (!).
  2. Less than (<), less than or equal to (<=), greater than (>), less than (> =).
  3. Equal (=), not equal (! =).
  4. Logic (&&).
  5. Logical OR (||).

Remarks:

  1. 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.
  2. 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.
  3. Types reference avoid excessive copying large elements.
  4. When the comparison operation, unless the comparison is a Boolean type, do not use true and false.

4.4 Assignment Operators

Fundamental contents:

  1. Assignment (=).
  2. In line with the assignment operator (+ =, - =, * =, / =,% =, << =, >> =, & =, ^ =, | =)

Remarks:

  1. Right assignment operator satisfies the associative law, the result is the left side of the assignment operator operand, which is a left-modifiable value.
  2. 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.
  3. Compound operator is equivalent to a = a op b;

4.5 increment and decrement operators

Fundamental contents:

  1. Increment (++), decrement (-).
  2. 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:

  1. 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.
  2. Post-increment (decrement) operator precedence> dereferencing operator. * P ++ is equivalent to * (p ++).

4.6 member access operator

Fundamental contents:

  1. Point operator, the arrow operator (.) (->).
  2. ptr-> men equivalent (* ptr) .men.
  3. Priority: dot operator, the arrow operator> increment decrement operators> dereferencing operator.

4.7 conditional operator

Fundamental contents:

  1. The conditional operator (? :)
  2. Conditional operator form: cond? exp1: exp2. If cond is true is executed exp1, otherwise execution exp2.
  3. Conditional operation nesting level is preferably not more than two to three.

4.8 Operators

Fundamental contents:

  1. Bit negated (-).
  2. Left (<<), right (>>).
  3. Bit and (&).
  4. Bitwise XOR (^).
  5. Bit or (|)

Remarks:

  1. Bit operand operator is an integer, and for setting the check bit.
  2. Bitwise operators try just for processing an unsigned integer with the sign bit signed integer no clear processing method dependent on the particular machine.
  3. 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:

  1. sizeof operator form: sizeof (type) or sizeof expr.
  2. Right sizeof satisfies the associative law, and the same operator precedence dereferencing.

4.10 comma operator

Fundamental contents:

  1. 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:

  1. 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.
  2. 显式转换:static_cast, dynamic_cast, const_cast, reinterpret_cast。

Remarks:

  1. Avoid cast.

4.12 operator precedence table

Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/103989583