C + + Primer fourth-expression

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators

Basis

Operators are divided into unary, two, and ternary operators depending on the number of operands, the same symbol may represent different operators, and the meaning of the specific symbol is determined by context. A generic operand can be automatically converted to the type required by the operator. Depending on the operator, the different operators can represent different meanings, called overloads of Operators.

An lvalue indicates that it can be placed to the left of the assignment operator and the right value cannot. When an object is used as an rvalue, the object's contents are used, and when an object is used as an lvalue, the object's identity (memory location) is used. Some operators must require that the operand be an lvalue, and the left value can be used as the right value.

Operators have different precedence and binding laws, and the parentheses have the highest precedence.

Most operators do not specify the order of evaluation, that is, the number of operands of the operator which evaluates first, and if the expression modifies the same object, an undefined error behavior is thrown. Such as:

int 0 ;     " " << ++i << Endl;

This is wrong because it is not possible to determine whether the compiler first asks for I or ++i, and the returned result is indeterminate. There are 4 types of operators that explicitly specify the order of evaluation of the operands, &&, | |,?:, comma operator.

The order of evaluation of operands is independent of precedence and binding law.

Arithmetic operators

Operands and evaluation results for arithmetic operators are rvalue, and the operands that participate in the remainder operation must be integers.

Logical AND relational operators

An operand with a value of 0 represents false, otherwise it is true, and for both types of operators, the operand and the evaluation result are right values.

Assignment operators

The left side must be a modifiable lvalue, the result of which is the left object, and the left value. The assignment operator satisfies the right-associative law, and the assignment operator has a lower precedence. When using the compound assignment operator, the operands on the left only have a value once and have a slight performance boost.

Increment and decrement operators

In the predecessor and post versions, the predecessor version returns the object itself as an lvalue, and the back version returns the copy of the object's original value as the right value. Unless necessary, it is recommended to always use a predecessor version.

*pbegin++, this notation is appropriate, first dereference the pointer, and then increment the pointer.

In particular, it is important to note that an operand can be evaluated in any order, *beg=toupper (*beg++), which is wrong and will result in a defined value because the order of evaluation to the left and right of the equals sign is indeterminate, and if the left evaluates first, the statement is equivalent to *beg=toupper (*beg). If the right is evaluated first, the statement is equivalent to * (beg+1) =toupper (*beg).

Member access operators

The point operator is used to access the members of an object, and if the object is an lvalue, the point operator returns an lvalue, and if the object is an rvalue, the point operator returns the right value. The arrow operator acts on the object of the pointer type, and the result is an lvalue. Obj.member equivalent to (*pobj). Member.

Conditional operators

The conditional operator evaluates only one expression in a two expression. The two expressions of the conditional operator are lvalue or can be converted to an lvalue of the same type when the result of the operation is an lvalue, otherwise the right value.

The conditional operator has a low priority, so it is best to use parentheses, such as cout<< (grade<60)? " Fail ":" pass "; cannot achieve the desired result, because will say hello to the front as a condition.

Bitwise operators

Bitwise operators operate on integers, providing the ability to set and check bits. Bit operations are not explicitly defined for the processing of symbolic bits, so it is recommended that bitwise operations be used only for unsigned numbers.

The left shift inserts a binary with a value of 0 on the right, and the right shift differs depending on the value inserted for the left operand, if the left is an unsigned integer, then 0 if the signed integer is inserted, 0 or insert 1 is determined by the environment.

Note that the shift operator evaluates the copy of the left-hand object, and the shift process may raise the operand.

The bitwise negation operator (~) converts the operand to the bitwise negation, and the operand of the char type is first converted to the int type and then the negation.

Bitwise AND, bit, or, or bitwise XOR performs the corresponding logical operation on a bitwise basis on two operand objects.

The shift operator satisfies the left binding law. The precedence and binding of an overloaded operator is the same as its built-in version.

sizeof operator

Returns the number of bytes of an expression or type name that satisfies the right binding law, and the resulting value is a constant expression of type size_t. The operator uses two forms, sizeof (type) or sizeof expr, to return the size of the expression evaluation result, and does not evaluate the expression. Performs sizeof on a reference type to get the size of the object of the referenced type, and the array executes sizeof to get the space of the whole arrays.

Comma operator

Contains two operands, evaluated sequentially in order from left to right, with ternary operators, and logical and logical OR operator-like order of evaluation of the operands. The comma operator returns the value of the right-hand expression, and if the right-hand expression is an lvalue, the result of the operation is an lvalue.

Type conversions

Implicit type conversions can be made between arithmetic types, and the design minimizes the loss of precision.

When unsigned types and signed types participate in an operation, if the unsigned type is not less than the signed type, the signed type is converted to an unsigned type, which can have side effects. If the unsigned type has all the values stored in the signed type, the unsigned type is converted to a signed type, otherwise the signed type is converted to an unsigned type.

In most expressions that use arrays, the array automatically transforms the pointer to the first element of the exponentially.

Literals 0 and nullptr can be converted to any type of pointer.

If the pointer or arithmetic type is 0, it is converted to false, otherwise it is converted to true.

A pointer or reference to a pointer or reference that can be converted to a constant point.

Avoid using forced type conversions.

C + + Primer fourth-expression

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.