The C ++ Programming Language Chapter 6 Notes

xiaoxiao2021-03-06  18

The C Programming Language Chapter 6 Note James Chen 050301 6.1 to 6.1.8 and other chapters are temporarily sluggish due to complicated difficulties. ********************* 6.2 Operator Overview ******************** The order is from top to next, from left to right class_name class name namespace_name namespace name member members object objects pointer pointer type type expression lvalue expr lvalue dynamic static dynamic static scope resolution class_name :: member global scope resolution namespace_name :: member :: name global :: Qualified-Name Member Select Object.member Member Select Pointer-> MEMBER Script Pointer [EXPR] Function Call EXPR (EXPR_LIST) Value Construction Type (Expr_List) increment Lvalue Recovery Lvalue - Type Identification TypeId (TYPE) Runtime Type Identification TypeId (Expr) Running Conversion Dynamic_cast (expr) Convert Static_cast (Expr) Does Reinterpret_Cast (EXPR) Const Convert CONST_CAST EXPR) Object Size SizeOf (Expr) Type Size SIZEOR (TYPE) Pre-increment Lvalue Pre-reduction --Lvalue Buy ~ Expr Non! EXPR One Dollar Negotic No. - EXPR One Dollar No. EXPR Address & Lvalue Indirect * EXPR Establishing New TYPE Establizes NEW TYPE (Expr_List) to establish Place new (expr_list) Type to establish placement and initialize New (expr_list) Type (expr_list) Destruction Release DELETE POINTER Destruction Array Delete [] Pointer Mandatory Type Conversion (Type) Expr Member Select Object. * Pointer-to-Member member Selection Object -> * Pointer-to-Member multiply Expr * EXPR in addition to EXPR / EXPR Tim (Expr] EXPR% XPR plus EXPR EXPR EXPR-EXPR left shift EXPR << EXPR right shift EXPR >> EXPR is less than EXPR EXPR is equal to EXPR> = expr> = expr = expr is not equal to EXPR! = EXPR Bit or Expr & Expr Bits or Expr | EXPR Bits or Expr | ^ EXPR Logic and EXPR & SPR Logic or EXPR || EXPR Condition Expression EXPR? EXPR: EXPR; Simple Assign EXPR = EXPR Multiply and Assign EXPR * = EXPR Separation EXPR / = EXPR Timed and assigns EXPR & = EXPR Plus EXPR = EXPR minus assignment EXPR- = expr = expr's right shift and assignment expr >> = expr & = expr or and assignment expr | = expr = expr = expr = expr Abnormal Throw Expr Common Expression EXPR, the operator in each interval has the same priority. The operator in the above interval is higher than the operator in the following interval.

For example: A B * C means A (B * C), not (A B) * C, because * is higher than . * P means * (p ), not (* p) . / / But I think it should be first * p, then P is just right. The one-dollar operator and the assignment operator are the right binding, and other operators are all combined. For example: A = b = c means a = (b = c), and the means of A B C is (A B) C. 6.2.1 Results The result type of the arithmetic operator is determined by the level of the "common calculation conversion" by one level. The overall purpose here is to make the result of the "maximum" arithmetic object type. If an object of the binary operator is a floating point number, the calculation is done by floating point operation, and the result type is also a floating point value. . If there is an object is long, the calculation uses long intensive arithmetic, and the result is also long. Better than INT, will be converted to int. As long as it is logically possible, a result of the arithmetic operator with the left value as an operation object is still referring to the left value of this left value. . INT x = 10, y = 100; INT Z = x = Y; // x = y after the value is x value cout << z << endl; // 100 cout << x << endl; // 100 COUT << Y << Endl; // 100 int * p = & x; // first x, then give & x to p // int * q = & x ; // wrong, at this time, X is not left Value, x ! = X, it does not place the result to X 6.2.2 Assessment order In an expression, the order of the sub-expression is not defined. In particular, you can't assume the expression from left to right. INT x = f (2) g (3); // Nothing to define f () and g (), who first call does not limit the order of the expression, so that it is possible to generate better code in a specific implementation. But sometimes it will also result in unfained results. INT i = 1; int A [3] = {11, 22, 33}; A [i] = i ; cout << i << endl; for (int J = 0; j <3; j ) cout << A [J] << endl; in the VC, compiling pass, no warning, A [i] = i is equal to A [1] = 1, but may also be A [2] = 1, it may also have a more strange behavior. Operators, commas, && logic, || logic, or to ensure that values ​​located on the left object must be evaluated before the object is operated.

If there is a comma expression, the value of the rightmost expression is the value b = (a = 2, A 1); // A 1 is (a = 2, a 1) Out of expression Value, b = 3, if it is logical, the left expression is true to the right expression int a = 2, b = 4; cout << (a> B && B> a) << endl; cout << B << Endl; // b = 4, because A> B is a fake, help b is not running, if it is logic or, the left expression is the right expression 6.2.3 operator priority IF (i <= 0 || Max = 0 && x <= 99) .... Also, don't use = (assignment) as == (equal), this big I can't make it wrong, I will laugh at people. . 6.2.4 Bit Logic Operators (Subscribe) 6.2.5 Incremental and Removal operators are used to directly represent increase, and they are reduced. Lvalue means lvalue = 1, which is equivalent to LValue = Lvalue 1 --LValue. and - can be used as a prefix or suffix operator. When used as prefix, the value of the expression is the value after the increment, and the value of the active object is also the value after the increment. When used as a suffix, the value of the expression is the value of the object, and then the value of the object is again increted. Such as: INT i = 3, A, B; A = i; // a = 4, i = 4, equivalent to i , a = i; b = i ; // b = 4, i = 5, quite At B = I, I ; , - can operate both the pointer or an array. Operators , - For the increase or decrease variables in the loop, the following is a string that is copied with 0 results. . .

Void structure (char * p, const char * {while (* p = * q ); // One line OK, concise and efficient} How to explain below. First look at a traditional string copy: intlength = strlen (q); for (int i = 0; i <= length; i ) p [i] = q [i]; seeking the line of Length is very wasteful, complete There is no need, because the string is ending with 0, so there is no condition I <= Length at all. So let's try another way: for (int i = 0; q [i]! = 0; i ) p [i] = q [i]; p [i] = 0; // use zero end The subscripted I can be removed, because P and Q itself is a pointer, can operate an array through or-to operate. While (* Q! = 0) {* p = * q; * p ; * q ;} * p = 0; after the increment operator allows us to use its value to increase, so: While (* Q! = 0) {* p = * q ;} * p = 0; due to expression * p = * Q is equivalent to * q , so: while ((* p = * q )! = 0); note There is no * p = 0, in this case, first do this expression * p = * q , when the last bit, 即 assign 0 to * p, then look at the entire expression Value, and * p = * Q value is the value of * p after the value, so it is not necessary to add end characters separately. Since * p = * Q is 0, it can be omitted! = 0 Judgment: while (* p = * q ); this is the most simple and fastest way to do what we want, so it can be seen It is reasonable to use in the loop, and you can make the program more streamlined more efficient. 6.2.6 Free Storage Named Object The time is determined by its scope. However, it is useful to establish a surplus time does not depend on the object of establishing its scope, which is useful. Sometimes I hope to build some objects in the function. After the end, I can still use the object, the operator New is excited, and the delete is responsible for destroying them, and the object assigned by the New is said to be "free storage." ", There is also a" heap object "or" allocated in dynamic storage. "

Such as: int * aa () {INT * a = new int (123); // Dynamically apply for an int, and initialize 123 return a;} int * bb = aa (); cout << * bb << endl; // 123 delete bb; // Destroy New Generated Object 6.2.6.1 An array can also use New to build an array of objects, such as: char * a = "i love bb !!"; char * b = new char [strlen a) 1]; // Dynamically apply for a char array STRCPY (B, A); cout << b << endl; delete [] b; // destroy B 6.2.6.2 memory consumption is exhausted, New What happens when you can't assign space? ? According to the default mode, a Bad_alloc exception will be thrown, such as: void f () {try {for (;;) new char [10000];} catch (bad_alloc) {cout << ", memory is finished !! < =, <=,> b) max = a; Else Max = B; Write the following will be better: max = a> b? a: b; switch equivalence with a set of IF statements: if (a = 1) a (); else if (a = 2) b () ELSE IF (a = 3) C (); ELSE D (); equivalent: switch (a) {case 1: a (); break; case 2: b (); break; casse 3: c () Break; default: d (); Break;} Both the same meaning, but more than Switch's version is more detected. Switch is easier to read for some complex statements.

6.3.2.1 Declaration in conditions In order to avoid accidentally erroneous use variables, the introduction variable in the minimum scope is a good idea, and it is best to delay the definition of local variables until you can give it an initial value. In this way, it will not occur because the troubles caused by the use of unmelted variables. One of these two principles is to declare the variable in the conditions: if (double d = aa) {aa = D;} Here, D is declared and initialized, the initialized D value is also the value of the condition . D 's scope of the D is from its declaration point, the statement that has been controlled to this condition ends, if this IF statement has an else branch, the ELSE branch is also a scope of D. Declaring variables in conditions, in addition to logic, it can produce a more compact source code. Declarations in conditions can only declare and single variables or const. 6.3.3 Iterative Statements While, for, DO..while (expr) For statement is to express the most standardized cyclic form. The expression of cyclic variables, end conditions, and update cyclic variables can be described in the "front" one line. This can greatly improve readability and reduce the frequency of error. If you don't need to initialize, the initialization statement can be empty. If the condition part is ignored, this for statement may be loop forever unless the user explicitly through Break, Return, Goto, Throw or some less obvious way, if you call an exit ( ), From the loop. If a loop is not a simple "introducing a loop variable, detection condition, update loop variable" type, then it is best to describe the While statement. The While statement simply performs the statement that is controlled until it changes to false. It is best to use the While statement instead of the for statement for those who do not have a clear cycle variable, or the update to the loop variable naturally appear in the middle of the cycle. Input cycles are a loop without a clear loop variable: While (CIN >> a) The DO statement is an incorrect and confusing roots. The reason is that the cyclic body is performed once before it is valued before the conditions. However, to make the circulation body work correctly, there must be some very like conditions must be established in the first time, and this condition does not necessarily establish it as expected. Try to avoid using the DO cycle. 6.3.3.1for statement Declaration for FOR (INT i = 0; I <4; i ) i's scope is for loop. 6.3.4goto C has a notorious goto statement, huh, BS says that nothing is notorious, it seems to be less useful. ********************* 6.4 Comments and indentation arranging ******************* 智 地Using comments, consistency use of indential prooflines, allowing reading and understanding of a program to become more enjoyable. Note It may be misused in many ways and thus seriously affect the readability of the program. Of course, the compiler will not understand the contents of the comment, so it cannot guarantee a note: 1, it makes sense. 2, describe this program. 3, it is in line with the current situation. Many programs contain uncomprivible, ambiguous, or at all, is wrong. Worse comments are not as good as notes. If some things have been understood by the language itself, then it should not be used as the content mentioned in the annotation. Such as: // Variable A must initialize // Variable a can only call this annotation by the F function too unnecessary, the program itself is already clear.

转载请注明原文地址:https://www.9cbs.com/read-48338.html

New Post(0)