C ++ primer Study Notes 4

Source: Internet
Author: User

C ++ primer Study Notes 4

Each expression has a result. The literal value constant is also an expression, and the result is the value itself. Except for special usage, the expression returns the right value. This is the left value, for example, ++ I. This expression is a left value, and the result of the comma operator is the DE value of the rightmost operator of the expression, if the rightmost is a left value, the entire comma expression is the left value.

The expression involves operators and operands. Operators have priority rules, associativity, and the order in which the operands are evaluated. In terms of the order in which the operands are evaluated, such as f1 () * f2 (); The return values of the two functions are multiplied, but it is not possible to determine which function to call first.

The operator % is "remainder" or "modulo", and the operand is an integer. The bool type is also acceptable.

For the "%" and "/" operators. If only one operand is negative, the results of the two operations (here only the absolute value, regardless of the symbol) depend on the machine; the symbols of the modulo operation also depend on the machine; however, the result of the division operation must be a negative number. For example:

21/-5: Depends on the machine. The result is-4 or-5.

21%-5: Depends on the machine. The result is 1 or-4.

In a logical expression, logic and logic always calculate the left operand first, and then the right operand. The right operand of the solver is used only when the result of the expression cannot be determined by the left operand. We call this calculation principle "short-circuit evaluation ".

Equal to the bool literal value. Since the value of true in bool can be converted to 1, it is difficult to judge whether a value is equal to true. Here we should consider the val type of a value. if val is of the bool type, the correct judgment is if (val ){... . If val is not of the bool type, it is determined to be equal to if (val = 1 ){...}; It is equivalent. if val is an integer, if (val) is a logical false when and only when val is 0. Other values, whether positive or negative, are logical.

The operand of the bitwise operator is an integer. The integer here can be either a positive integer or a negative integer. But for the negative integer symbol bit processing method, different machines have different implementations, so try to use unsigned positive integers as the bit operation operations.

The left-shift and right-shift operations in bitwise operations. For example, if it is unsigned, the value shifts left to expand, and the value to the right to fill 0; if it is shifted right to decrease, the value on the left to fill 0. For a symbol, during the right shift, the left side may be filled with 0 to change it to a positive number, or the left side may be supplemented with 1 without changing its symbol, which depends on the machine.

Cout <42 + 10; correct, because + is higher than <

Cout <(10 <42); correct, because there are brackets

Cout <10 <42; error because < <高于关系运算符。cout<<10之后返回一个ostream的引用,返回引用的目的是可以连续输出,达到流的效果。将一个ostream引用和42对比较是无意义的。< p>

The right combination of the value assignment operation.

Intival, jval; ival = jval = 0; this is legal. The value assignment operator also has a return value. Run jval = 0 to return an integer and then assign it to ival. The value assignment operator has a low priority.

Where judgments and assignments are prone to errors. If (I = 42) is actually true. The literal explanation is to assign 42 to I and then judge whether I is true or false. But this is obviously meaningless. This Code seems to have been written incorrectly. The original intention is if (I = 42 ). To avoid this hidden error, you can write it as if (42 = I). if you write less "=" if (42 = I), it is a compilation error, timely detection.

How to exchange values of two numbers.

Arithmetic Method: a = a + B; B = a-B; a = a-B;

Bit operation method: a ^ = B; B ^ = a; a ^ = B;

For a deeper discussion of I ++ and ++ I, there are multiple auto-increment and auto-increment operations on I in a long expression. The results of such expressions depend on machines. There is a concept that the expressions in any language cannot be evaluated, that is, the order point. When executing this sequence, you must ensure that the subexpression before the sequence has obtained the result. In C ++, it is specified that the object value is modified at most once between the previous and next order points. The machine implementation determines where the order points are set in the expression. It is worth noting that in a long expression, do not try to modify an object multiple times.

I ++ is the right value, and ++ I is the left value.

The specific implementation of I ++:

Constint int: operator ++ (int)

{

Export ldvalue = * this;

++ (* This );

ReturnoldValue;

}

++ I Implementation Details:

Int & int: operator ++ ()

{

* This + = 1;

Return * this;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.