[C ++ programming foundation] (1) - the function prototype declaration, the function template, quote, const often quoted, const constant pointer

First, the function prototype declaration:

1, the function declaration tells the compiler function name, and how to call a function (return type and parameter); function definition provides the actual body of the function.

2, Mandatory: In C ++, if the function of the position before the function definition, requires the function prototype declaration must be made to the called function before the function call.

3, in a function declaration, the parameter name is not important, only the parameter type is required. I.e., the following two writing are permitted:

you max ( you get, you y);
you max ( you , you );

4, when the function definition (func.cpp) a code body (main.cpp) separately, added in main.cpp the #include "func.cpp" silent out can call functions directly in the code file body.

 

Second, the function template

1, the type of parametric : ① known, value data can be transmitted through the function parameters, define the data value function is unknown, the received argument to the function call is determined only until its value.

   ② Also, the type of data may also be transferred through parameters. Not denote a particular data type in a function definition, when a function call occurs, the compiler automatically infer parameters passed in the real data type.

2, the term template function, actually creates a generic function, it uses the type of data (including the return type, parameter types, the type of local variable) may not specify, but with a virtual type instead of ( in fact, it is a placeholder identifier), such as to re-introduce the real type of arguments passed to the function call according to the inverse occurs. This generic function is called a function template (Template Function) .

#include <iostream>
using namespace std;

// generic function definition 
template <typename T1>
T max(T a,T b,T c)
{
    if (b>a) a=b;
    if (c>a) a=c;
    return a;
}

3, the same, the number of types of parameters may be determined as required:

template<typename T1, typename T2 >

 

Third, the reference

1, parameter: if the function parameter to be used, it must declare the variable parameter value received. These parameters become the formal parameters of the function. Like other formal parameters local variables declared within the function as created when entering the function, destroyed when the function exits.

2, the function is called, there are two passing arguments to functions and the method of operation can be returned to the argument:

① pointer calls: the address parameter to a parameter copy; ② a reference: the reference parameter into the parameter copy.

* Call-by will not affect the argument. It is only a one-way transmission, the value of the argument is passed parameter.

3, in C ++, "reference" variable is the alias variable, and therefore reference is also known as an alias (alias).

  Cited wording:     type name & quote name = same type of a variable name;

int n-= 40 ;
 int & n-r =;     // declare a reference variable r is an integer variable, which is initialized to n- 
COUT << r;      // get 40

  # R is a reference, it can be said that the type int & r

4, a reference to a variable declaration does not open up another memory unit, r and n represent the same variable unit.

5, when a reference statement, while it must be initialized: commonly used to initialize a variable reference; may be initialized with reference to a reference; eventually be used expression returns the value of a variable expressions to initialize referenced as follows:

1  // returned reference value as a function of 
2  int n-= . 9 ;
 . 3  int & the SetValue () { return n-;}
 . 4  int main ()
 . 5  {
 . 6      the SetValue () = 90 ;
 . 7      COUT n-<<;     // Output 90 
. 8      return  0 ;
 . 9 }

Explain here the third row: SetValue () function returns a value of type int &, i.e., return type is a reference value; and the return value is n, i.e., the SetValue () returns a reference to the value of n.

Returns reference value advantage is that it can be placed to the left of the equal sign, the specific role of the future encounter repeat.

 

Four, const const reference: to be defined with reference to a const.

int i = . 5 ;
 const  int & i = a;     // declare often referenced 
a = . 3 ;         // try to modify a constant reference value of i, the error

Often cited by reference not to modify the value of the original variable, but the original variables they can change.

 

Five, const constant pointer

1, define a constant pointer:    const type name * pointer variable name;

2, a constant pointer, a pointer to the content can not be changed. Its content can not be modified by a constant pointer pointing, but can be modified in the pointer.

int n-, m;
 const  int * P = & n-;
 * P = . 5 ;    // compiler error, can not modify the data pointed 
n-= . 4 ;     // OK, the content itself may vary point 
P = & m;    // constant pointer the point can vary

4, constant pointer can not be assigned to non-const pointer, otherwise you can (not recommended):

. 1  const  int * P1;
 2  int * P2;
 . 3 P1 = P2;     // OK 
. 4 P2 = P1;     // error 
. 5 P2 = ( int *) P1;     // OK, cast

Amount of the pointer is pointing (can be changed) to obtain a constant pointer (not changed), you can modify the value of pointer const, but const pointer points to a value in most cases tend not to be modified.

5, to protect the value of the constant pointer variables constant:

void MyPrintf(const char* p)
{
    strcpy (P, " the this " );     // compiler error 
    the printf ( " % S " , P);      // OK 
}

 strcpy () function is expecting a char * variable, whereas here p is a const char * variable, so the compiler error. Constant pointer p is not protected is modified.

Guess you like

Origin www.cnblogs.com/hsh17/p/11735972.html