Expressions and statements for C + +

Source: Internet
Author: User
Tags constant continue execution integer reference

An expression

1, the function call should be treated as an operator, which has no limit on the number of objects participating in the operation.

2, about "left value (lvalue)" and "Right value (rvalue)" Can make a simple generalization: When an object is used as the right value, the object's value (content) is used, and when the object is used as the left value, the object's identity (where it is in memory) is used.

Examples of more specific left and right values: C + + Priemr 5ed P121

3, the precedence of operators sets the way in which the operands are grouped, but there is no indication of the order in which the operations are evaluated, and in most cases the order of the values is not explicitly evaluated, for example, for the following expression:

int i= F1 () + F2 ();

It is unknown whether to call F1 first or call F2 first. Therefore, for this operator to avoid multiple operations objects to modify the same variable together, as in the following code, the final output is not determined:

int i=0;

cout<< i << "" << ++i << Endl; Not defined

But there are 4 operators in C + + that explicitly specify the order in which the operations are evaluated: Logic and "&&", logic or "| |", condition (?:) operator, comma operator ",". This is why we do not typically overload these operators in our own class types that we define.

4, short-circuit evaluation: the logic and operators and logic or operators are first to find the value of the left Operation object and then the right operation object value, when and only if the left operand can not determine the results of the expression, the right-hand operator will calculate the value of the Operation object.

5, the difference between i++ and ++i

The post operator needs to first save the original value, then i+1, and then return a copy of the original value, and the front operator, just add 1 to the original value, and then return. So ++i is more efficient than i++, of course if I is an int type or pointer, the compiler optimizes i++, but not if it is an iterator for other class types or complex classes.

6, note that the dereference operator and the + + operator priority, in the actual code for simplicity often write * (i++) as *i++. Because + + has a higher precedence than the dereference operator.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

7, when using the conditional operator, try to avoid writing a depth-nested conditional operator. In addition, the precedence of the conditional operator is very low, and the parentheses are used in the expression, such as:cout<< (I<J?I:J);

8, about the sizeof operator. The result of the sizeof is a compile-time constant, noting the value of the following code:

1 int a[10];

2 int* p = A;

3 int n1 = sizeof (a)/sizeof (*A); n1=10

4 int n2 = sizeof (p)/sizeof (*P); N2=1

sizeof operator Tips:

1 performs a sizeof operation on a char or an expression of type char, with the result of 1.

2 perform sizeof operations on reference types to get the size of the referenced object's year space.

3 performs the sizeof operation on the pointer to get the size of the space occupied by the pointer itself.

4 the size of the space occupied by the pointer to an object that is obtained by sizeof operations on the dereference pointer does not need to be valid.

5 the size of the space occupied by an array of sizeof operations is equivalent to performing a sizeof operation for all elements of the logarithm group and summing the resulting results. Note that the sizeof operation does not convert the array into pointers to handle.

6 performing a sizeof operation on a Sting object or vector object returns only the size of the fixed part of the type, and does not calculate how much space the element in the object occupies.

9, when you evaluate a compound expression, pay special attention to the precedence and binding of the operators. In particular,! = = = = Priority is less than <=,>= and other relational operators.

10, type conversion

1) Implicit conversion

In the following cases, the compiler automatically converts the type of the Operation object:

1 in most expressions, an integer value smaller than the int type is first promoted to a larger integer type.

2 in the condition, the non-Boolean value is converted to a Boolean type.

In 3 initialization, the initial value is converted to the type of the variable; In an assignment statement, the right-hand operand is converted to the type of the left-hand Operation object.

4 if arithmetic or relational operations have multiple types of operations, they need to be converted to the same type.

5 When a function is called, the type conversion between the actual participating formal parameters.

6 class types can define some conversion functions.

2) Explicit conversion

1 Statci_cast<type>: any explicitly defined type conversions, as long as the underlying const is not included

2 Const_cast<type>: Only change the underlying const of the Operation object

3 Reinterpret_cast<type>: Usually provides a lower level of interpretation for the bit pattern of an operational object.

4 Dynamic_cast<type>: Run-time type recognition.

Statement

1, like most languages, C + + provides conditional execution statements, circular statements that repeatedly execute the same code, and jump statements used to interrupt the front control flow.

2, when using block statements, note that variable scopes defined within a block statement are within a block area only. In particular, variables that are initialized or defined in a control statement, such as an if or a for statement, have only the scope of the block area.

The use of 3,switch statements. The case label must be a shape constant expression and not allow the variable to be defined within the switch statement if there are cases or default statements underneath it, because in some situations, the execution variable defines the following case branch in cases where there are no instances where the definition of the variable is executed. Unless the variable is defined within the code block.

4, in the For loop, if there is a continue statement, the statement that follows the continue is skipped, but the change statement for the counter in the For statement is not skipped.

5, RANGE for statement

The scope for statement is the newly introduced content of c++11, which can traverse all the elements of a container or other sequence. Its grammatical form is:

1 for (declaration:expression)

2 statement;

Where expression must be a sequence, such as a list of initial values enclosed in curly braces, or vector or string type objects. The common feature of these types is that they have the begin and end members that can return the iterator.

declaration defines a variable in which each element of the sequence must be converted to the type of the variable. The easiest way to ensure type compatibility is to use the auto type descriptor, which enables the compiler to help us specify the appropriate type. If you need to perform a write operation on the elements in a sequence, the loop variable must be declared as a reference type.

Here is an example of traversing a vector with the scope for statement.

1 Vector<int> v = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

2 for (auto &r:v)

3 {

4 R *= 2;

5}

When you use the scope for TIME statement, you cannot increase the vector object's elements through the scope for statement. Because the value of end () is stored in the scope for statement. Once you add (delete) an element to a sequence, the value of the End function may become invalid.

Author: ☆ronny

Source: http://www.cnblogs.com/ronny/

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.