The process of learning C + +, pointers are difficult, familiar with the pointer, there is a very painful pain, it is the function pointer. This blog post details the common various functions of the pit daddy pointers.
As for the detailed study of pointers, this post is recommended for C + + pointers
As with data, functions have addresses, and the address of the function is the starting address of the memory where the function language code is stored. The function pointer is pointing to this address. The type that the function pointer points to is the function itself. We know that the pointer to the type represents the size of the memory area pointed to by the pointer. So the type that the function pointer points to is the size of the memory that the function occupies in memory. Knowing the starting address and size of the function, the function pointer can easily replace the function to complete the function call.
One, the simplest function pointer
Variables include declarations and assignments, pointers are no exception, and function pointers are no exception. Let's look at a simple function:
void Add (intint b) { << a + b << Endl;}
A simple addition to the function that computes and outputs to the command line.
So how do you call it through a function pointer?
1. Statement:
void (*P1) (intint b);
The declaration of a function pointer is simple, basically replacing the function name with a pointer. The type of pointer P1 is void (*) (int a,int b), indicating that the pointer is a pointer to a function with a type of void () (int a,int b) pointing to the pointer
2. Assignment Value:
P1 = add;
3, can also be directly defined:
void (*P1) (intint b) = add;
Note that the function name add of the function void Add (int a,int b) is the address of the function. Assigning the address add to the pointer P1, you can call the function directly through the function pointer p1.
4. Call:
(*P1) (12);p 1 (12);
Attention! For historical reasons, all 2 of these methods can be called functions.
two, an array containing multiple function pointers
Sometimes this is the case, there is an array, each element in the array is a function pointer, how to define this array?
1. Explanation *p[n] and (*p) [n]
We know that the [] operator takes precedence over *, so p[3] represents an array with 3 elements, and *p[3] precedes "*" indicating the type of the element in the array, that is, *p[3] represents an array of 3 pointers.
P[3] Represents an array of 3 elements, then (*P) [3] is replaced with *p p, it is easy to think, (*P) [3] represents a pointer to an array containing 3 elements.
2. Statement:
void (*p2[2]) (intint b);
The array is named P2, the array size is 2, the element type in the array is void (*) (int a, int b), indicating that the element is a pointer to a function, and that the pointer is of type void () (int a,int b).
3. Assignment Value:
p2[1] = add;
Understanding is the same as above.
4. Call:
p2[1] (2,3);(*p2[1]) (3,4);
There are also 2 different ways to do it.
third, pointer to "array containing multiple function pointers"
The title seems to be a bit of a mouthful. In short, this pointer points to the "array containing multiple function pointers" above. In fact, it is very simple, to put it bluntly, is to replace the P2 in the above with a pointer.
1. Statement:
void (* (*P3) [2]) (intint b);
Can see, nothing is to replace P2 with *P3.
2, assignment, note that since it is a pointer, it must be initialized before use:
P3 = &p2;
(*P3) [1] = add;
Attention! Since the essence is to replace P2 with *P3, c++11 can be very simple to define directly: Auto P3 = &p2; Replace void (* (*P3) [2]) (int a, int b) = &p2;
3. Call:
(*P3) [1] (12);((*p3) [1]) (12);
C + + function pointers