4th Chapter Expressions

Source: Internet
Author: User
Tags bitwise

4.1 Basics

Left and right values

When an object is used as an lvalue, the object's memory space is used. When an object is used as an rvalue, the memory value of the object is used.

An lvalue can also be used as an rvalue to use its value. But the right value is not necessarily a left-hand value.

Warming

An assignment operation requires an lvalue as the left operand, and an lvalue is returned.

Decltype acts on an lvalue, returns a reference type, such as *p, returns an Lvalue, and Decltype (*p) gets the int& type. Decltype acts on the rvalue and returns a pointer type, such as &p, which returns an Rvalue, Decltype (&p) gets the int** type.

4.2 Arithmetic operators (+-*/%)

Arithmetic operators are grouped in left-to-right order, which is the left-bound law.

The search result for arithmetic operators is the right value.

In arithmetic operations, non-numeric values need to be converted to numeric values to participate in the operation, for example bool values are converted to 0, 1, respectively.

For the% operator, the action object should be an integer and satisfy the m= (m/n) *n+m%n. The m%n symbol is the same as M.

4.3 Logical AND relational operators (>=<, = =,! = 、!、 &&, | | )

The types returned by the logical and relational operators are Boolean types, and the operands and evaluation results are right values.

4.4 Assignment Operator (=)

The left object of the assignment operator must be a modifiable left-hand value.

The assignment operator returns his left operand and is an lvalue.

The assignment operator satisfies the right binding law.

Use of the result of the assignment operator operation:

int i;  while  the )    do_something ();

4.5 Increment Decrement (+ + 、--)

The predecessor, increments/subtract the operand, and returns the object after the operation as the left value. Back, increments/subtract the operand and returns a copy of the object before the operation as the right value.

Therefore, if it is not necessary to avoid using the Post object, you can reduce the overhead of generating a copy.

Post-use:

Auto Pbeg = v.begin ();  while (Pbeg! = v.end ()    ) // + + priority, where the current value is output and the iterator is promoted to a location    cout<< *pbeg++<<endl;

4.6 Member access operator (,.)

Ptr->men equivalent to (*ptr). Mem

4.7 Bar operator (cond?) EXPR1:EXPR2)

The conditional operator uses the right associative law, which is a right-to-left combination. The right side is usually the left branch.

4.8-bit arithmetic

~ Bitwise negation
<<, >> shift operations (sometimes IO operations)
&, | Bitwise-AND, or
^ Bitwise XOR OR

The shift operation satisfies the left binding law. The priority is higher than the relational operation, assignment operation and condition operation, which is lower than arithmetic operation.

4.9 sizeof operator

Returns the number of bytes occupied by an expression or type name. A constant that satisfies the right binding law and the resulting size_t type. There are two kinds of forms:

sizeof (type)
sizeof expr

sizeof does not directly evaluate the value of an expression.

sizeof *p satisfies the right-binding law, and sizeof and * have the same priority, so it is equivalent to sizeof (*P). Because the value of an expression is not evaluated directly, the value can still be evaluated even if p is a null pointer.

Warming

sizeof evaluates the char expression to 1.

sizeof the result of an array operation is the number of bytes occupied by the entire set.

sizeof only returns the size of the fixed part of the type to string and vector, and does not return the size of all its elements occupying space

4.10 comma operator (,)

Evaluates from left to right, returning the value to the right.

4.11 Type Conversions

Implicit conversions

Integer type smaller than int, promoted to a larger integer type.
Condition, non-Boolean values are converted to Boolean values. In initialization, the initial value is converted to the type of the variable, and the right type is converted to the left type in the assignment.
Arithmetic operations, relational operations, are converted to the same type of operation. byte is the same, the signed conversion is unsigned. Bytes are different and are converted to more than a byte type.
function is called.

The array is converted into pointers.

Show transformations

Form:cast-name<type> (expression) that enforces type conversions

Supplement :const INT *const p = &value, where the second const is the top layer and limit P's memory cannot be changed; the first const Is the underlying, limited-pointing memory cannot be changed.

Static_cast: Can be used as long as the underlying const is not included.

Const_cast: Can only change the underlying const property, cannot change the type, that is, you can convert the constant to a variable.

Reinterpret_cast: Provides a re-interpretation of the memory bit pattern, such as a pointer to int (int *ip) that transforms the exponentially pointer to char (reinterpret_cast<char*> (IP) ).

int x (0x00484848);  // hexadecimal 48, indicating H int *p=&x; Char *c=reinterpret_cast<char*>(p); cout<< c <<endl;        // The output is: HHH, dependent on the machine, where the high point of INT is the high memory address. 

dynamic_cast: Run-time type recognition.

Legacy Coercion Type conversions

Type (expr)

(type) expr

4th Chapter Expressions

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.