As with general data, functions also occupy a certain amount of space in memory, so functions also have the concept of "address", which refers to the "address" (that is, the starting address of the function code block), and the type of the function pointer is the function itself. Knowing the address, we can easily rely on the function pointer to complete the function call.
1. Simple function Pointers:
1 void Add (intint b) {//write a very simple function here 2 cout << A + b << Endl; 3 }
1 void (*P1) (intint b); Declares a function pointer to a function with the type "return void, two int parameter" of function 2 p1 = add; Assignment 3 //void (*P1) (intint b) = add; You can also assign a value, a meaning of 4 (*P1) (12); Here are two methods of invocation, which are equivalent for a historical reason = =5 p1 (12);
2. An array containing multiple function pointers:
1 void (*p2[2]) (intint b); Declaration 2 p2[1] = add; Assignment 3 p2[1] (2,3); Call, the same two methods can be 4 (*p2[1]) (3,4);
3. Pointer to an array containing multiple function pointers:
A little bit of a mouthful, this pointer points to the previous point in the "array containing multiple function pointers".
1 void (* (*P3) [2 ]) (int A, int b); Nothing is to replace the P2 with *p3 2 p3 = &P2; The pointer must be initialized before the 3 (*P3) [1 ] = add; 4 (*P3) [1 ] ( 1 , ); Call 5 ((*P3) [1 ]) (1 , 2 );
C++11 can be very simple to define directly: Auto P3 = &p2; Replace void (* (*P3) [2]) (int a, int b) = &p2;
function pointer Small note