C ++ Primer: Chapter 6 summary


Chapter 6 Functions

6.1 basis function

Fundamental contents:

  1. Function definition: return type, function name, parameter list, the function thereof.
  2. Calling function and the called function, parameters, and arguments, return type of the function.
  3. Local variables: internal parameter function body defined variables. Automatic object: the object exists only during block execution. Local static variables: the first time when using the initialization, even if it ends in a function has been performed, have to wait until the program terminates was only destroyed.
  4. Function declarations (function prototypes): can only be defined once, you can be declared many times. Function without declaring the function body, end with a semicolon.
  5. Separate compilation.

Remarks:

  1. Function returns the type of void, showing the function returns no value. Function can not return an array, or function, but the function returns a pointer to an array or a pointer.
  2. Local variables are only visible in the function scope, it will hide the name (domain name declared in a role with the same name will hide entity declaration outer scope).
  3. Local static variables of built-in type if not explicitly initialized, the initialization value is performed, the variable initial value 0.
  4. If the function is never been used, the function can only declare not defined.
  5. Functions should be declared in the header file, the function defined in the source file, the source file and the header file should be included.

6.2 parameter passing

Fundamental contents:

  1. Value is passed: pass parameter non-reference type, may be a pointer parameter.
  2. Passed by reference: reference arguments.
  3. const and parameter arguments: other arguments involved in the initialization process to initialize the same shape.
  4. Parameter array, the array reference parameter, passing multidimensional arrays.
  5. main: processing command line options.
  6. Deformable argument function, ellipsis parameter.

Remarks:

  1. Passing a pointer value can be transmitted, the pointer is a real parameter participating in two pointers point to the same objects in different pointers. Modifying the pointer parameter does not change the argument pointer, the pointer modifying parameter referents argument pointer does not change, but its referent would be changed.
  2. Argument passed by reference can be modified to avoid the copying of large objects, additional information can be returned. It recommended instead of a reference parameter pointer parameter.
  3. Common reference pointer and ordinary type matching parameter used only argument; binding constant reference parameter can take any of the expression of energy conversion.
  4. Try to use the binding constant references to avoid misleading arguments can be modified, while expanding the function can accept arguments range.
  5. The array is passed three methods: the closing tag array itself; pass a pointer array to the first element and the rear tail element; defining parameter represents a size of the array.
  6. argv [0] to save the program name, real user input parameter start from argv [1].
  7. Two methods of reference deformable transfer: transmitting initializer_list parameter; using a variable parameter template.
  8. initializer_list object element will always be a constant value, can not be modified.

6.3 The return type and a return statement

Fundamental contents:

  1. No return value of the function, the function has a return value.
  2. Returns an array of pointers.

Remarks:

  1. If the cycle contains a return statement, the following cycle should also have a return statement.
  2. Function returns a reference, in fact, is to return an alias cited object.
  3. Do not return to a local object reference or pointer. Since the end of the function, local variables, and the space occupied by the pointer has been released, no longer points to a valid memory area.
  4. Call a function returns a reference value obtained left, the other to get the right type of return value.
  5. Function returns a list of values ​​flower brackets included.
  6. Recursive function that is directly or indirectly calls itself the function. Recursive function must have a path that does not contain recursive calls, that there must be a recursive termination condition, there would be a recursive loop. main functions can not call itself.
  7. Function returns a pointer to an array method: direct return pointer array; TypeAliases; the decltype return type declaration; tail opposing return type.

6.4 Function overloading

Fundamental contents:

  1. Overloaded functions: the same scope, the same function name, different parameter list.

Remarks:

  1. main function is not recursive, not overloaded.
  2. Overload function simply amounts or parameters different shape parameter types, the return type is not considered, not look parameter name, const ignored top, bottom distinguish const.
  3. Three possible results call overloaded function: the best match; no match; ambiguous call.
  4. Do not put function declarations local scope, because it hides the same name entity outer scope.

6.5 special-purpose language features

Fundamental contents:

  1. Default argument, inline function, constexpr function.
  2. assert preprocessor macro, NDEBUG preprocessor variable.

Remarks:

  1. Has a default value of a parameter, all subsequent parameter must have a default value; default design when the argument of function, usually placed in front of the modified parameter, the parameter change infrequently put back; repeatedly stated function, but in the given scope, it can only be given a default argument; except for local variables, the general expression of the type could be used for matching default argument.
  2. Inline functions to be added before the return type inline, can avoid the overhead of a function call.
  3. constexpr function requires return type and parameter types are literal types (integers and floating point) , and only a return statement. constexpr functions are implicitly designated as inline functions. constexpr shape function parameters and return values are not necessarily constant expression.
  4. Constexpr inline function and functions generally defined in the header file, multiple definitions may be, but must be defined exactly.
  5. If expr is true, assert nothing to do; if it is false, assert output and terminates the execution of the program. Commonly used to check the assert macro conditions "can not happen".
  6. Pretreatment variables and macro names must be unique within the program.
  7. When defining NDEBUG can avoid running cost required for the inspection conditions, inspection equivalent operation is not performed.

6.6 Matching function

Fundamental contents:

  1. Candidates and viable functions.
  2. Best match, no match, ambiguous call.
  3. Argument type conversion.

Remarks:

  1. The candidate with the function called function with the same name; its statement visible in the call site.
  2. Viable function-size parameter is equal to the amount of real parameter quantity, shape or participating in the same argument can be converted to the parameter type argument types.
  3. That is the best match possible shape to participate in the function argument type that best matches, no matching function that is not found viable functions, ambiguous line of the function call can be no best match.
  4. Best Match Rating: exact match (the same type, or an array of function pointers turn, deletions top const)> const conversion> type lifting> type conversion arithmetic> class type conversion.

6.7 function pointer

Fundamental contents:

  1. I.e., the function pointer to a function pointer.

Remarks:

  1. When the function address-pointer assignment operator is optional.
  2. Required function pointer type parameter and return type exact match, conversion of pointers to functions to initialize the absence or assignment.
  3. Value as a function, automatically converted to pointers. But does not automatically return type conversion function into a pointer, displays must return a pointer to the specified type.
  4. A function pointer as a return value or parameter, is generally used type of alias, decltype. If the return value, set the end return type may be used.
Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/104058736