Function pointer of C ++ learning notes
Similar to data items, functions also have addresses. The function address is where the memory where the machine language code is stored starts. I. Basic knowledge of function pointers. Suppose you want to design a function named estimate () to estimate the time required to write the code with the specified number of lines. In addition, you want different programmers to use this function, in addition, this function allows each programmer to provide their own algorithms to estimate the time. To achieve this goal, the mechanism adopted is to pass the algorithm function address that the programmer wants to use to estimate (), and the following must be done: obtain the function address. Declare a function pointer and use the function pointer to call the function. obtain the function address and use the function name (not followed by the parameter. For example, if think () is a function, think is the address of the function. To pass a function as a parameter, you must pass the function name. Be sure to differentiate whether the address passed is the function address or the return value of the function: 1 process (think); // transfer the address of the function think () to process () 2 thought (think (); // pass the return value of the think () function to though () 2. the declaration function pointer declaration should specify the return type of the function pointed to by the pointer and the feature mark (parameter list) of the function. Assume that Pam has compiled a function to estimate the time. Its prototype is as follows: 1 double pam (int); // if the function declaration is correct, the pointer type declaration is as follows: 1 double (* pf) (int); 2 // pf points to an input int parameter, return a double-worthy function. We can see that the function name in the function declaration is replaced with * pf, so pf becomes the pointer to the function, which is known by the basic knowledge of the pointer, (* pf) is also a function. * Pf must be enclosed in parentheses in the Declaration; otherwise, 1 double * pf (int); this is equivalent to declaring a function pf, and its return value is a pointer to double, it is not the same as the function pointer we want to declare. After the pf is declared correctly, the corresponding function address can be assigned to it: 1 double pam (int); 2 double (* pf) (int); 3 pf = pam; now the pointer pf points to the function pam. It is emphasized that the feature mark and return type of pam () must be the same as that of pf. Assume that you want to pass the number of lines of code and the address of the estimation algorithm (such as pam () function) to estimate (). The prototype is as follows: 1 void estimate (int lines, double (* pf) (int); to enable estimate () to use the pam () function, you need to pass the pam () Address to it: 1 estimate (50, pam ); 3. as mentioned above when using pointers to call functions, (* pf) assumes the same role as the function name. Therefore, when using (* pf), you only need to regard it as the function name: 1 double pam (int); 2 double (* pf) (int); 3 pf = pam; 4 double x = pam (4 ); // call the function 5 double y = (* pf) (5) using the function name; // call the function with the pointer pf. However, C ++ also allows the use of pf: double y = pf (5) Like the function name; // the same effect as double y = (* pf) (5) Why is pf equivalent to (* pf? Because there are two views, one school holds that, because pf is a function pointer and * pf is a function, (* pf) () should be used as a function call; another school holds that since the function name is a pointer to the function, the behavior of pointing to the function pointer should be similar to that of the function name. Therefore, pf () should be used as a function call. It seems to be a reasonable statement, so C ++ made a compromise-both methods are correct, although logically conflicting. Ii. function pointer example copy code 1/* function pointer example */2 # include <iostream> 3 4 using namespace std; 5 6 // Declaration of the two functions to be called 7 double betsy (int); 8 double pam (int); 9 10 // estimate () declare 11 void estimate (int lines, double (* pf) (int); 12 13 int main () 14 {15 int code; 16 17 cout <"Enter the number of lines of code:"; 18 cin> code; 19 cout <"Betsy Evaluation Result: \ n"; 20 estimate (code, betsy); 21 cout <"Pam Evaluation Result: \ n"; 22 estimate (code, pam); 23 return 0; 24} 25 26 double betsy (int lns) 27 {28 return 0.05 * lns; 29} 30 31 double pam (int lns) 32 {33 return 0.03 * lns + 0.0004 * lns; 34} 35 36 void estimate (int lines, double (* pf) (int) 37 {38 cout <lines <"line code will take time "; 39 cout <(* pf) (lines) <"hours \ n"; 40}