C ++ Primer 5th learning notes (3), primer learning notes
C ++ Primer 5th learning notes (3)Chapter 4/5 highlights and difficultiesYou can click here to review chapter 3Because Chapter 5 has a small amount of content, it is merged with the notes in Chapter 4.Chapter 4 refers to the knowledge related to expressions. Expressions are the infrastructure of C ++. This chapter consists of three parts: 1. Basic expression conceptsIncluding the basic concepts of expressions, the concepts of left and right values, the priority combination Law, and the order of values.2. Various OperatorsIt mainly includes arithmetic, relational, logical, value assignment, ascending and decreasing, member access, conditional, bitwise, sizeof, and comma operators.3. type conversion, Including implicit and explicit conversion rules.The following are the knowledge points of this chapter: Knowledge Point 1: P120, 4.1.1, basic concepts of expressionsAn expression is composed of one or more computing objects. When multiple objects constitute an expression, the objects are connected by operators to form a complex expression. In an operator, two objects and operators are required to connect to form an expression. This operator is called a binary (binary, binary) operator. To analyze an expression, you must first understand the meaning of the computing object, the priority Union Law of the operators, and the order in which the operators are evaluated.1.For complex expressions that contain subexpressions, you should evaluate them in order to see which subexpression value should be obtained first.2.For a less complex subexpression, you should view each operand (object) in the expression based on the priority and perform operations with that operator first.3.If there are operators with the same priority in both the left and right sides of the same computing object, the values of the expressions should be calculated from right to left or from left to right in the combination order according to the combination law.Knowledge Point 2: P121, 4.1.1, left and right Origin:The left and right values are originally concepts in C language, specifically the expressions in the left and right sections of the value assignment operator. In C language, the object that can be placed on the left of the value assignment operator is the left value, and the object on the right of the value assignment operator is the right value. The meanings of these two concepts in C ++ have changed.Overview:The concept of left and right values in C ++ can be summarized for the moment. In terms of nature, when an object is set to the right value, we use the content of this object. when an object is set to the left value, we use its location in the memory. Application: some positions in the expression need the left value, while others need the right value. The values of the expressions are also left and right. In the value assignment operator, the left operand and expression result are both left values. The operation object with the address operator is the left value, and the right value is obtained. The Return Value of the unreferenced and subscript operators is the left value. When decltype acts on the expression, if the expression result is a left value, decltype returns a reference type.Knowledge Point 3: P122, 4.1.2, priority and combination Law1. When an operation object in a complex expression is connected to multiple operators, which operator has a high priority is used to calculate the operator and the value after the operation of the object. 2. when an operator object in a complex expression connects multiple operators with the same priority, according to the combination law corresponding to this priority, calculates the value of an expression in the order of right-to-left or left-to-right. For example, 3 + 2*4-7. This expression is a complex expression. Because the priority of "*" in the expression is relatively high, 2*4 is calculated first and 3 + 8-7 is obtained; the new expression is more concise, with only the +-two symbols remaining. The two symbols have the same priority. Therefore, we can see that the combination of the two symbols matches the left combination. Therefore, from left to right, 11-7 is displayed. Expected result 4 is displayed.Knowledge Point 4: P123, 4.1.3, Value OrderIn an expression, if the operation object is returned by a function, the state of the object needs to be known after calculation. The priority of the function call symbol is the same on the right. In the middle, the operation object is connected by several other symbols with lower priority, for example, int a = f () + g (); is f () called first or g () called first? The answer is:Undefined. The C ++ syntax does not specify who should first and later in this case. In the expression ++ I ++ below, the expressions with the highest priority + + I and I ++ are separated by operators with lower priority +, this is undefined about whether ++ I is calculated first or I ++. Because this expression calculates ++ I first or the result of first I ++ is different, therefore, this expression is incorrect. If a variable is changed multiple times in the same expression, the order in which the expression is evaluated is not necessarily the same, so there will be ambiguity. This should be avoided. Currently, only four operators specify the order of value. 1. logical operators & |: these operators calculate the value of the left operand first. 2. Conditional operators? : The condition operator calculates first? And evaluate, and then evaluate the value of the expression on the left and right. 3. Comma OPERATOR: the order in which values are calculated is from left to right.Knowledge Point 5: induction of operators, left and right valuesIn this chapter, the attributes of the values returned by the expressions formed by various operators and the attributes of the operations required by the operators are as follows: arithmetic/logical/bit operators: The operation objects and results are both right value assignment operators: the operation object on the left must be the Editable left value. · the operation object on the right is the right value, and a left value is returned. Increment/decrease OPERATOR: returns the left value for the front version ++/-- and returns the right value for the front version ++. The operation object must be the left value. Arrow member access operator: acts on the pointer and returns the left value of the expression. Point member access operator: the object to which the member belongs is the left value and the result is the left value. The object to which the member belongs is the right value and the result is the right value. Conditional OPERATOR: When all three expressions of the conditional operator are left or can be converted to left, the result is left; otherwise, the result is right.Knowledge Point 6: P125, 4.3, division and modulo resultTwo non-floating-point variables/literal values are separated, and the result is still of the original type. The original operand is not an integer, and the result is a decimal number. In C ++ 11, for the Division operator, the result is rounded to zero (the decimal part is directly cut, and the resulting number is the result ). For the modulo operator, the result symbol is the same as the divisor symbol.Knowledge Point 7: P136, 4.8, bitwise OperatorAfter the Left shift operator moves the binary number, it will insert zero to the right. The specific behavior of the right shift operator when processing the operands of the signed type (especially those with a negative number) is determined by the environment.Knowledge point 8: P157, 4.9, sizeof OperatorThe sizeof operator can be used in two ways. The first is to add an expression statement directly after sizeof, and the second is like sizeof (type name ); in the second form, the space occupied by the class object is obtained. In the first form, if the expression is of the pointer type, the sizeof operator returns the size of the pointer. When a class named data has a member named student, you can use the scope identifier to interact with sizeof and use sizeof (data: student) to calculate the number of student nodes.Knowledge Point 9: P141, 4.11, implicit type conversionOverview: In C ++, some types can be converted to each other according to certain rules. In many cases, two or more identical types are required in the context to continue the operation. Therefore, values of one type are automatically converted to values of another type. This process is implicit conversion, where arithmetic implicit conversion is more common. Major implicit conversions:1,In most expressions, the type smaller than the int type is promoted to the int type.2,In conditions, non-boolean values must be converted to boolean values.3,In the initialization and assignment statements, the type of the object on the right of the assignment symbol is converted to the type of the object on the left for calculation.4,In arithmetic/relational operations, objects of multiple types are converted to the same type.5,Convert the form parameter to the type of the real parameter (Chapter 6 ).6,The array name is converted to a pointer.7,0, nullptr will be converted to any type of pointer. Any type of pointer can be converted to (const) void * type. Supplement to the implicit conversion when the fourth arithmetic conversion occurs: In addition to Arithmetic Operators, different operands must be converted to the same type for calculation. Take the I + a; expression as an example to understand the arithmetic conversion method.1,First, when the types of I and a are smaller than int, such as char and short, convert them to int type. If the maximum value of their original type is greater than the maximum value of the int type in the current system, it is converted to the unsigned int type.2,After that, if I and a are of the same type, end arithmetic implicit conversion. If I and a are of different types, convert the objects of the smallest type into objects of the most byte type.3,If the maximum value of the signed type that occupies more than one byte is smaller than the maximum value of the converted object, the signed type is converted to the unsigned type.Knowledge Point 10: P144, 4.11.3, display ConversionCast-name <type to be converted> (value to be converted) has the following format: one of static_cast, dynamic_cast, const_cast, and reinterpret_cast.Static_castIt is used for common forced type conversion. Static_cast can be used as long as two types are associated, such as the floating point type and integer type, integer type and boolean type, Boolean Type and pointer type. Only the constant const cannot be converted to the variable.Const_castRemove (or add) the underlying const of the object. The type to be converted and the type to be converted must be pointer or reference type. This function is often used in function overloading described in chapter 6.Reinterpret_castDepending on the machine, a type is forcibly changed to another irrelevant type. Dynamic_cast supports runtime type recognition, which will be mentioned in Chapter 19.Chapter 5 is about statements, and statements are also an important component of C ++. This chapter consists of three parts: 1. Concepts of statementsIncluding the concepts of simple statements and statement scopes.2. Condition/loop/jump statementThe condition statements mainly include if/else statements, switc statements, and :? Expression Condition statements; loop statements are for statements and while statements; jump statements include continue, break, and goto statements.3. try/throw and Exception HandlingIncluding how to handle exceptions.The following are the knowledge points of this chapter: Knowledge Point 1: P155, 5.2, statement ScopeThe block enclosed in curly brackets is the identifier of the scope. Objects defined in the scope only play a role in the scope. The block cannot access or control the internal variables of the block. Especially in the switch statement, the execution process of the switch may span some labels. when an object is declared and defined in the label, the scope of the object will be extended to all the case labels, if case 1 defines int a; switch executes case2, then the statement we do not want to execute produces its own scope, which is obviously not feasible. Therefore, you can use braces to form a block after the case tag, so that no scope problem occurs. The same is true for goto. You cannot skip object definitions before (the next few lines of code. It is not allowed to pass the definition of a variable to the scope of the variable. However, the goto statement can be skipped (the first few lines of code.Knowledge Point 2: P172, 5.6, try statement block and Exception HandlingTry \ throw and catch can be used for exception handling in the following format: try {block to be detected} // the block to be detected contains throw statements to throw an exception catch (Object Name of an exception-type object) {Exception Handling statement} catch (same as above, many catch statements can be written.) {another set of exception handling statements} the header files that throw exceptions and catch exceptions are defined in stdexcept. The statement that throws an exception is like the throw exception type ("exception text"). The exception type generally only supports values, initialization, and calling operations such as member function. what.