Defined
each function takes up a memory unit, they have a starting address, and a pointer to the function's entry address is called a function pointer.
Grammar
data Type (* pointer variable name) (parameter table):
Int (*myfunc) (double b, int c);
Description
The data type in the defined form of a function pointer is the type of the return value of the function.
Differentiate between the following two statements:
Int (*p) (int a, int b);//p is a pointer variable that refers to a function, the return value type of the function is an int
*p (int a, int b),//p is the function name, and the return value type of this function is an integer pointer
A pointer variable that points to a function is not fixed to which function, but merely denotes a variable of that type, which is specifically used to hold the entry address of the function; the address of which function is assigned to it in the program, and it points to which function.
When assigning a value to a function pointer variable, you simply give the function name without having to give the argument.
For example, the prototype of the function max is: int max (int x, int y); The pointer p is defined as: Int (*p) (int a, int b); The role of P = max is to assign the entry address of the function max to the pointer variable p. At this point, p is the pointer variable pointing to the function Max, where p and Max point to the beginning of the function.
In a program, the pointer variable p can point to a different function, but a function cannot be assigned to an inconsistent function pointer (that is, a function pointer cannot be directed to a function that is inconsistent with its type).
If you have the following function:
int fn1 (int x, int y); int fn2 (int x);
The following function pointers are defined:
Int (*P1) (int a, int b); Int (*P2) (int a);
The
P1 = fn1; Correct
p2 = fn2;//correct
p1 = fn2;//Generate compilation Error
After defining a function pointer and having it point to a function, a call to a function can be called by a function name or by a function pointer (that is, a pointer variable that points to a function).
such as the statement: c = (*p) (A, b); Represents the call to the function (max) that is pointed by P, which is a,b, and the value of the function given at the end of the function call is assigned to C.
A function pointer can only point to the entry of a function, and it is not possible to point to a command in the middle of a function. You cannot use * (p+1) to represent the next instruction of a function.
One of the common uses of function pointer variables is to pass pointers to other functions as arguments.
Use examples for function pointers
Note To see the code comments can be
#include <iostream> using namespace std;
Class Test {public:test () {cout<< "constructor" <<endl;
int fun1 (int a, char c) {cout<< "This is fun1 call:" <<a<< "<<c<<endl;
return A;
} void Fun2 (double d) const {cout<< "is fun2 called:" <<d<<endl;
Static double Fun3 (char buf[]) {cout<< "This is Fun3 call:" <<buf<<endl;
return 3.14;
}
};
int main () {//class static member function pointers and C pointers are used identically double (*pstatic) (char buf[]) = null;//does not need to add class name pstatic = TEST::FUN3;//Can not add address symbol
Pstatic ("Myclaa");
Pstatic = &test::fun3; (*pstatic)
("XYZ"); Normal member function int (test::* pfun) (int, char) = NULL; Be sure to add class name Pfun = &test::fun1;
Be sure to add the address symbol test mytest; (Mytest.*pfun) (1, ' a '); Call is to be sure to add the class to the object name and the * symbol//const function (the basic ordinary member function is the same) void (test::* Pconst) (double) const = NULL;
Be sure to add a const PCONST = &test::fun2;
Test Mytest2; (Mytest2.*pconst)
(3.33); A pointer to a constructor or destructor,Seemingly not, do not know whether the C + + standard does not have to point to the two functions of the pointer//(TEST::* pcon) () = NULL;
Pcon = &test.test;
Test mytest3;
(Mytest3.*pcon) ();
return 0;
}