Function pointers, function pointers, and pointer Functions
1. Use of function pointers
Int fun (int a, int B); // declare a function int (* p) (int, int) // define a function pointer, p is a function pointer, the int at the beginning refers to the return value type of the function, followed by the return value type of the function in brackets // assign a value to the function pointer p = fun; // p points to the function p = & fun; // and p = fun are equivalent. // Several equivalent forms of function call int a = p (1, 2 ); int B = (* p) (1, 2); int c = fun (1, 2 );
2. Several Forms of function pointer Definition
Defines the function pointer pointing to int fun (int a, int B ).
1) direct definition:
Int (* p) (int, int );
P = fun; // or p = & fun
2) use typedef to define the type alias:
// P1, p2 is a function pointer type
Typedef int (* p1) (int, int );
Typedef decltype (fun) * p2;
P1 f1 = fun; // here p1 is a function pointer type. You need to define a specific function pointer object f, and f is a pointer to the function.
P2 f2 = fun; // same as above
// Here, p1 and p2 are function types,
Typedef int p1 (int, int );
Typedef decltype (fun) p2;
P1 * f1 = fun; // f1 is a function pointer
P2 * f2 = fun; // f2 is a function pointer
3) use using to define the type alias
Using p1 = int (*) (int, int); // p1 is a function pointer type.
Using p2 = int (int, int); // p2 is a function type.
P1 f1 = fun; // f1 is a function pointer
P2 * f2 = fun; // f2 is a function pointer
3. function pointer Parameters
1) The function name is used as the form parameter, and the function name is automatically converted to the function pointer.
# Include <iostream> using namespace std; int sum (int a, int B) {return a + B;} void fun (int (* p) (int, int )) {cout <p (2, 3) <endl;} int main (int argc, char * argv []) {fun (sum ); // sum is automatically converted to function pointer return 1 ;}
2) define the function type alias
# Include <iostream> using namespace std; int sum (int a, int B) {return a + B;} typedef int f (int, int); void fun (f p) // f is the function type, and the form parameter is automatically converted to the function pointer type {cout <p (2, 3) <endl;} int main (int argc, char * argv []) {fun (sum); // here sum is converted to function pointer return 1 ;}
3) define a function pointer alias
# Include <iostream> using namespace std; int sum (int a, int B) {return a + B;} typedef int (* f) (int, int ); void fun (f p) // f is the function pointer type, and p is the function pointer {cout <p (2, 3) <endl;} int main (int argc, char * argv []) {fun (sum); // sum is automatically converted to function pointer return 1 ;}
4. returns the pointer to the function.
1) define directly
Int (* f (int) (int, int) // f (int) Is the function name and parameter list, and the return value type is int (*) (int, int)
2) Adopt the tail Return Method
auto f(int)->int(*)(int,int)
3) Use a type alias to define the function pointer type
Tpyedef int (* p1) (int, int); using p2 = int (*) (int, int); typedef int p3 (int, int ); using p4 = int (int, int); // equivalent declaration p1 f (int); p2 f (int); p3 * f (int); p4 * f (int );