Function (residual) - C ++ review (6)

A, return statement

       1. Non-return reference type, the return value is copied to the expression;

       2. returns a reference type, then return the object itself;

       3. Do not return references to local variables , local variables because the function exits will disappear.

Second, the static (static) local variable

       If a variable is static local variable, the variable is initialized once have existed until the end of program execution.

Third, inline functions *

       Call the function much slower than solving expressions. Then for short program fragment, adapted to be specified for inline functions; this reduces overhead written functions.

1 aaa.h
2 inline int max(int a,int b)
3 {
4     return a>b?a:b;
5 }

       Typically inline function into a header file; If implemented as a function (defined) in the header, even if not added in front of inline, that is inline function .

Four, this pointer *

       Each class member function (static member functions excluding) has an additional, this implied; formed in this function is called, the called function is initialized to the address of the object .

Fifth, const member functions

       If the parameter back plus a list of member functions const keyword, then this pointer is a pointer to the members of the const object, the member function can not change the value of the object is called:

. 1  void setName ( String name) const ;
 2  
. 3  void the Person :: setName ( String name) const 
. 4  {
 . 5      the this -> name = name; // is called the this object is actually * the Person ; can not change the value
 6 } // this compile-time error, operation type is: const std :: string * = std :: string, an error

Sixth, the constructor

       And the name of the same class; no return value (void even no); the role is to initialize member variables when creating a class object will call the constructor;

       The constructor initializer: after the constructor parameter in shape, between the colon and {} is the initialization part of the list, such as:

1 Per(int pid,string name,int age):_pid(pid),_name(name),_age(age){};

       Used to initialize member variables directly.

Seven, function overloading

       This simple, refers to the same function name but different function parameters or different types to determine which function to call in the end parameter when calling.

       Note that the return value can not constitute a heavy load function.

Eight function pointer ***

       It refers to a pointer to the function rather than a pointer to an object .

       A function pointer to a specific type, determined by the return value and the list of parameters, such as: int (* max) (int, int); max is a function pointer is a (return value is int, the parameter list is int, int )The function.       

. 1  int max ( int , int );
 2  
. 3  int (* max) ( int , int );
 . 4  
. 5  can simplify the definition function pointer by typedef
 . 6 typedef int (* m) ( int , int );
 . 7 mA = max ; // corresponds to a function pointer variable 
. 8  
. 9  int R & lt max = ( . 1 , 2 ); // call the function by the pointer, two equivalents 
10  int R & lt = a ( . 1 , 2 );

       Function pointer parameter, you can achieve callback (how not used):

 1 void f(void (*x)(int,int))
 2 {
 3     x(1,2);
 4 }
 5 
 6 
 7 void f2(int a,int b)
 8 {
 9     int x=a+b;
10 }

       Description: To call the f function, you need to pass a pointer such as f2, due to the function name represents a function pointer, then the name of the function f2 can be directly passed in the past, that is, f (f2), f2 callback refers to the inside of the parameter ( int a, int b) is the f (x (1,2)). Call the procedure: first call f, f required function pointer, we will pass over f2, then x (1,2) back to call f2, this is a callback.

       Return pointer to a function (how not used)

 

Guess you like

Origin www.cnblogs.com/jiang-021/p/11524982.html