Home » C++ » C++ primer seventh chapter - function

C++ primer seventh chapter - function

1, parameter passing of function

The initialization of the

parameter is the same as the initialization of the variable..

if the parameter has a non reference type, the value of the copy argument is initialized. The function does not access the arguments passed by the call itself, so it does not change the value of the argument.

if the parameter is a reference type, it is only an alias of the argument, when the parameter changes, and the corresponding reference argument changes;

if the parameter is a pointer type, it can change the pointer to the value.

2, the limitation of replicating arguments

is not suitable for copying arguments, including:


1) when a function needs to modify the value of an argument;

2) when you need to pass large objects as arguments, the time and storage cost of copying objects is often too large for practical applications,.

3) when there is no way to implement object replication,.

3 uses const references to avoid copying


1) if a function has an ordinary non const reference parameter, it is obviously not possible to call through the const object because the function can modify the passed object, which violates the const property of the argument,.

2) if only for the purpose of reference is to avoid copying the argument, then the parameter should be const reference. If the parameter such as non const reference, there is no necessary to limit the use of function. For example:

string:: size_type find_char (string, &s, char, c) {
String:: size_type I = 0;
While (I = s.size) and s[i] (!! = C)
Return i;
program to find in a string object in a specified character. This function will be its string argument as a non const common reference, although this function does not modify the values of the parameters. This definition problem is not through the literal string to use this function:

if (find_char ("Hello world", ' o' //error
)) Summary: should will not need to modify the reference parameters for the const reference. The ordinary non const reference parameter in the use of less flexible. This parameter can be used neither const nor object initialization, using literal values or rvalue the expression of the argument to initialize.

4, vector, and other container parameter

generally, functions should not have parameters such as vector or other standard library container type. Calls to functions that contain vector parameters of the normal non const reference will assign each element of vector to.

C++ programmers tend to pass containers by passing iterators to the elements that need to be handled in the container rather than passing the container reference.

void print (vector:: const_iterator, beg,
Vector:: const_iterator, end) {
While (beg = = end) {
Cout "*beg++";
If (beg = = end)
Cout "endl";
5, array parameter

1) cannot copy arrays;

2) when an array name is used as a parameter, the array name is automatically reduced to a pointer to its first element,.

3) three methods for passing arrays:

(1), void, print (int*), {}

(2) void print {}

(int [])

(3) void print (int [10]) {} / / the compiler will automatically ignore any array parameter specifies the length. The number 10 has no meaning.


is not hungry, it can pass arrays directly, but the parameters of a function can be written in an array. These three definitions are equivalent, and the parameter types are int, *.

4) we can define an array parameter as a reference to the array, if the parameter is an array parameter, the compiler does not convert an array argument into a pointer, but a reference to the array itself. In this case, whether the type and size of the compiler checks an array argument matching with.

type and size parameter

6, do not return references to local objects (pointers)

when the function is finished, the storage space allocated to the local variable is released. At this point, the reference to the local object (pointer) will point to the indeterminate memory.

7, the default argument is

1) the default argument is specified by providing explicit initial values to the parameter in the parameter list. If a parameter has a default argument, all parameters subsequent to it must have the default argument.

2) design functions with default arguments, where part of the work is to arrange the parameters so that the least used default arguments are at the head, and the default arguments are most likely to be at the back of the.