C ++ Primer learning summary chapter 1 functions, Chapter 4 primer
Chapter 2 Functions
1. Local variables in the outermost scope of a function cannot use the same names as function parameters because they belong to the same scope.
2. lifecycle of local static variables:Entire ProgramThe execution pathTermination of entire programIs destroyed. During this period, even if the function where the object is located is executed, it will not be affected.
3. If the parameters of the overloaded function are different from those of the top-level const, the following error occurs:
If there is a difference between the underlying const, it can be regarded as heavy load.
4. If the function parameter needs to be referenced (and does not change the value of the referenced object ),It should be defined as a constant reference.Because the use of common reference parameters will limit the type of arguments accepted by the function.
In the preceding example, if a constant int is passed to the get () function, an error occurs.
There is anotherMore noticeable errorsIf function A correctly defines the shape parameter as A constant reference, but function B still defines the shape parameter as A normal reference, then using function B in function A will fail:
5. How to pass FunctionsArray parameters?
The array is also an object, but the array name will be automatically converted to the first element pointer. The array also has an address. The array also has a pointer. The array pointer can be obtained by using the * solution reference.
One-dimensional array:
Multi-dimensional array: C ++ does not actually have a multi-dimensional array. The so-called multi-dimensional array is just an array, so the 2nd-dimensional (And all subsequent dimensions) Is part of the array type and cannot be omitted.
Problem:Why should I use (* A) [I] instead of A [I] to output the value of array A above?
A is A pointer to the int [10] array. * A is an array of int [10. then (* A) [I] is the I-th element of the int [10] array.
Or you can understand this.In fact, A is the first element (This element isInt [10] Array), So * A is the first element of (This element is an int [10] array.), SO (* A) [I] is the specific int value element.
The pointer to the first element (array a [0]) of array a is passed to the print () function. to output 10 elements of array a [1], A + 1 is required first, then * (A + 1), and then take each element(* (A + 1) [I].
6.In-depth discussion:When the array name is used, the array name is automatically converted to the pointer of the first element of the array. but what if we want to use the pointer to the array (not just the pointer to the first element?
Array is also an objectAnd use the & get address operator to get its address:
7. What is the meaning of A char ** A parameter?
In the following example, "A is an array and all char * pointers in the array" is not rigorous. A correct understanding should be that A is A pointer to A char pointer. So we * A can get A char * pointer, as for whether the char * Pointer Points to the element of a char array or whether the char * Pointer Points only to a char or whether the char * pointer content is invalid, it is not something we can manage, this requires other parts of the program to be guaranteed.
Note the implementation of the above program.
8. How to pass FunctionsArray reference parameters?
The array reference parameters must correctly specify all dimensions of the array.
9. Do not return references or pointers to local variables (Although simple programs can often get the correct results when doing so, it is a serious error.):
10.CallOperator andPointOperator,ArrowOperatorSame priorityAnd all complyLeft joinSo if the function returns a pointer, a reference, or a class object, we canAccess the members of The result object with the result of function call.
11. C ++ 11 supportList initialization Return Value:
12. The function cannot return an array (because the array cannot be copied). The function can only return an array pointer:
13. How to define a function that returns an array pointer directly?
The aboveNo fewer parentheses.
14. const_cast usage:
Usage:Const_cast <type_id> (expression)
This operator is used to modify (Bottom Layer) Const or volatile attribute. Except for const or volatile modification,Type_id and expression are of the same type..
1. Constant pointers are converted to very large pointers and still point to the original object;
2. constant reference is converted into a very large number of references and still points to the original object;
From the above s3, we can see whether the strong conversion of const_cast <> () still maintains the previous memory address,
Const_cast <> () can cause the underlying const to disappear,But the memory address remains unchanged.. After using const_cast <> (), you can use B to change the memory content, but still cannot use a to change it. the underlying const of the reference and pointer has the same effect with strong conversion.
If you only use const_cast <> () to forcibly convert to the top-level const, it is meaningless and compilation fails.
Is usedThe value of a is assigned to B,Even if a is converted to the int type, there is no difference.
Error: invalid use of const_cast with type 'int', which is not a pointer, reference, nor a pointer-to-data-member type.
The above is a compiler error description. It indicates that const_cast can only be used for pointers or references to the underlying const. (what is the third item ?)
15. How do I declare functions with only local scopes?
When a function is defined globally but declared in a local scope, a specific function can only have a local scope. main is not visible to print, because the definition of print is behind the main function, and the declaration of print is in the get function. if print is defined before main, print can also be used even if print is not declared.
16. function default real parameters. If a function parameter has a default value, each parameter must have a default value. The following two methods are used to define the default value:
Method 1: This method is recommended.
Method 2
And if you call a function that contains the default value,Real parameters always match the leftmost parameters first.. Local variables cannot be used as default arguments.
17. Only global variables can be used as the default real parameters. The default real parameters are resolved within the scope of the function declaration.The evaluate process occurs when the function is called..
18. const and constexpr modifier:
We already know that the value of a literal constant cannot be changed, so it is called a constant. A variable is a storage space in the memory. We can read and write the content in it. However, sometimes we also need a mechanism to prevent random modification of the value of a variable. In this case, we need to use the const-qualifier ). When defining or declaring a variable, the const qualifier can be placed before or after the type specifier we mentioned earlier, indicating that the value of the variable cannot be changed. Note that initialization is required if the variable definition or declaration has a const qualifier. Because the const variable cannot be assigned a value later. For basic data types, this is also a subtle difference between initialization and assignment: You can initialize a const variable, but cannot assign values. For example,
The const qualifier is also a type specifier in syntax, but it cannot be used independently. It must be used together with other types specifiers to indicate the unchangeable (constant) nature. Because the const qualifier is also a type specifierConst variables and non-const variables are not of the same type in C ++.Although they are still closely related, we will detail them later. What you need to remember now is that int and const int are not of the same type.
C ++ also provides a more ''strict''Constexpr specifier(Constexpr-specifier). When a variable is declared or defined, if the constexpr specifier is used before or after the type specifier, it indicates that the variable is a const variable and must be initialized. Maybe you said, isn't this const the same?Constexpr is more strict. The expression used to initialize the variable must be a ''constant expression ''. That is to say, the constexpr variable is a const variable initialized by a constant expression.. For the constant expression, we will discuss it in detail later. For the basic data type, the constant expression is a variable restricted by the nominal value constant and constexpr, and a const variable initialized by a constant expression. In other words, the value of a constant expression can be determined during compilation.
The value of the constexpr variable must be the CompilerIt can be determined during compilation.. In the above example, the nonconst_var value may be changed in terms of syntax, so it cannot be determined during compilation, and it is not a constant expression.Because const_var2 is initialized by a very number expression, const_var2 is not a constant expression.However, the definition and initialization of const_var2 is legal. Constexpr is more strict than const. It is not a constant expression for initializing constexpr_var2 and constexpr_var3, so their definitions are incorrect.
19. function pointer and usage:
When the function name is used as a value, the function is automatically converted to a pointer.
Declare a function with function pointer parameters:
Use typedef to simplify the definition of function pointers:
C ++ primer 5th Chinese edition P222-223 page has more content expand.