C++primer Study note "4"

Source: Internet
Author: User

Each expression has a result, and the literal constant is also an expression, and the result is the value itself. The result of an expression other than a special usage is the right value. There are also cases of lvalue, such as ++i; the expression is an lvalue, and the result of the comma operator is the leveled value of the rightmost operation of the expression, and if the rightmost is an lvalue, the entire comma expression is the Lvalue value.

Expressions involve operators and operands, operators have precedence rules, there are associativity, and the Order of evaluation of operands. In terms of the order of evaluation of operands, such as F1 () * F2 (), the return value of two functions is multiplied, but it is not possible to determine which function to call first.

The operator% becomes the "redundancy" or "modulo", the operand is an integer type, where the bool type is also possible.

For both the "%" and "/" operators, say. If only one operand is negative, then the results of both operations (only absolute values, regardless of symbols) depend on the machine, and the symbol of the modulo operation depends on the machine, but the result of the division operation is definitely negative. As an example:

21/-5: Dependent on the machine, the result is-4 or-5

21%-5: Dependent on the machine, the corresponding result is 1 or 4

In a logical expression, logical and logical OR always evaluates the left operand first, and then the right operand. The right operand of the solver is only available if the left-hand operand cannot determine the result of the expression. We call this operation principle "short-circuit evaluation".

Equality test with BOOL literals. Since true in BOOL can be converted to 1, it is difficult to tell if a value is equal to true. Here to consider the type of a value Val, if Val is of type bool, then the correct judgment is if (val) {...}; Can. If Val is not of type bool, then the judgment is as if (Val ==1) {...}; If Val is an integer, then if (Val) is a logical false when Val is 0, the rest of the value, whether positive or negative, is logically true.

The operand of the bitwise operator is an integral type, where the integer type can be either a positive integer or a negative integer. But for the symbolic bit processing of negative integers, different machines have different implementations, so try to use unsigned positive integers as operands of bit operations.

The principle of left and right shift operations in bit operations. Unsigned, the left shift is the numerical expansion, the right side is 0, the right shift is the numeric decrease, and the left side is 0. For a signed, the right side of the process, the left may be 0 to turn it into a positive number, or perhaps 1 does not change its symbol, which depends on the machine.

cout<<42 + 10; correct, because + above <<

cout<< (< 42); also correct, because there are parentheses

Cout<<10 < 42; error because << is above the relational operator. After cout<<10 returns a reference to the ostream, the purpose of the return reference is to continuously output, reaching the effect of the stream. It is meaningless to compare a ostream reference with a 42 pair.

The right-associative nature of the assignment operation.

Intival, Jval; ival = Jval = 0; This is legal. The assignment operator also has a return value, executes jval= 0 first, and returns an integer to the ival. The assignment operator has a very low priority.

Judgments and assignment errors. If (i = 42) This sentence is actually true, the literal explanation is to assign 42 to I, and then judge I for real or false. But this is obviously meaningless, and this code is like being written incorrectly, which is intended to be if (i== 42). In order to avoid such concealment errors, it can be written as if (= = i) so that if you write down a "=" if ("=") is a compilation error, can be found in time.

A value about how to exchange 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 self-increment and self-subtraction operations on I in a long expression, and the result of this expression is dependent on the machine. There is a concept that the expression of any language can not escape the evaluation, that is the order point. The order point intuitively means that the sub-expression before the order point must be guaranteed to be the result when executing to this order point. In C + +, the value of an object is modified up to 1 times between the previous and next order points. As for where to set the order point in the expression, there is a machine implementation decision. It is worth saying that you should not attempt to modify an object multiple times in a long expression.

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

Specific implementations of i++:

Constint int::operator++ (int)

{

Intoldvalue = *this;

+ + (*this);

Returnoldvalue;

}

Implementation details of the ++i:

int&int::operator++ ()

{

*this+= 1;

Return*this;

}

C++primer Study note "4"

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.