Function pointers are usually used to implement callback. The basic usage is as follows:
1. Define the function pointer type
// Define a function pointer prototype: int fun (int );
Typedef int (* ptrfun) (INT apara );
2. function pointer variable definition
Ptrfun pfun; // pfun is the function pointer variable name.
INT (* pfun2) (int A); // pfun2 is also the name of the function pointer variable.
3. The function pointer is passed as a function parameter.
// Define the callback function
Int callback (int ){
Return ++;
}
// Define the callback function
Void caller (ptrfun CB)
// Void caller (INT (* CB) (INT) // you can declare
{
Int npara = 1;
Int nret = CB (npara );
}
// Use callback
Void test (){
Caller (callback); // directly use the callback function
Ptrfun cb = callback; // int (* CB) (INT); Cb = callback;
Int nret1 = CB (99); // nret1 = 100;
}
4. function pointer usage
// Define the pointer of the function pointer
Typedef int (** ptrptrfun) (INT apara );
// The pointer of the function pointer as a parameter
Void ptrcaller (ptrptrfun CB)
// Void ptrcaller (ptrfun * CB) // pointer Declaration
// Void ptrcaller (INT (** CB) (INT) // prototype Declaration
{
Int nret = (* CB) (999); /// nret = 1000;
}
// Use the pointer of the function pointer
Void test (){
Ptrfun cb = callback;
Ptrcaller (& CB );
}
5. Use of function pointer Arrays
// Define the array of function pointers
Ptrfun farray [10];
// Int (* farray [10]) (INT); // prototype Definition
For (INT I = 0; I <10; I ++ ){
Farray [I] = callback;
Int nret = farray [I] (I); // nret = I + 1;
}
6. function pointer size
// Since it is a pointer, the size of the pointer in a 32-bit system is 4
Int nsz1 = sizeof (ptrfun); // nsz1 = 4;
Int nsz2 = sizeof (ptrptrfun); // nsz2 = 4;
Note:
The compiler has multiple call specifications. For example, in Visual C ++, you can add _ cdecl before the function type, _ stdcall or _ Pascal indicates the call specification (default value: _ cdecl ). The call specification affects the given function name generated by the compiler, the sequence of parameter passing (from right to left or from left to right), and the responsibility for Stack cleaning (caller or called) and parameter transfer mechanism (stack, CPU register, etc ).
Bytes ------------------------------------------------------------------------------------------------
Function pointer and typedef
Usage of function pointers in C ++ (including the usage of typedef)
(1) simple function pointer application.
// Form 1: return type (* function name) (parameter table)
Char (* pfun) (INT );
Char glfun (int A) {return ;}
Void main ()
{
Pfun = glfun;
(* Pfun) (2 );
}
The first row defines a pointer variable pfun. First, according to the "Form 1" mentioned above, we realize that it is a pointer to a function. This function parameter is of the int type, and the return value is of the char type. We cannot use this pointer only in the first sentence, because we have not assigned a value to it.
The second row defines a function glfun (). This function is a function that returns char with int as the parameter. We need to understand the function at the pointer level-the function name is actually a pointer, and the function name points to the function's Code The first address in the memory.
Then there is the cute main () function. You should have understood its first sentence-it assigns the address of the function glfun to the variable pfun. In the second sentence of the main () function, "* pfun" is obviously the content of the address pointed to by pfun. Of course, the content of the function glfun () is taken out, and the given parameter is 2.
(2) typedef is more intuitive and convenient.
// Form 2: typedef return type (* New Type) (parameter table)
Typedef char (* ptrfun) (INT );
Ptrfun pfun;
Char glfun (int A) {return ;}
Void main ()
{
Pfun = glfun;
(* Pfun) (2 );
}
The function of typedef is to define a new type. The first sentence defines a ptrfun type and defines this type as a pointer to a function. This function takes an int as a parameter and returns the char type. You can use ptrfun like Int or char.
The code in the second line defines the variable pfun using this new type. In this case, the variable can be used as in Form 1.
(3) Use function pointers in C ++ classes.
// Form 3: typedef return type (Class Name: * New Type) (parameter table)
Class ca
{
Public:
Char lcfun (int A) {return ;}
};
CA;
Typedef char (CA: * ptrfun) (INT );
Ptrfun pfun;
Void main ()
{
Pfun = Ca: lcfun;
CA. (* pfun) (2 );
}
Here, the definition and use of pointers are both subject to "class restrictions" or "objects ", it is used to indicate that the function pointing to the pointer is the class object of that class, which can also be obtained by using new. For example:
Ca * PCA = new CA;
PCA-> (* pfun) (2 );
Delete PCA;
In addition, this class Object Pointer can be a member variable within the class, and you can even use this pointer. For example:
Class CA has a member variable ptrfun m_pfun;
Void CA: lcfun2 ()
{
(This-> * m_pfun) (2 );
}
In a word, the class member function pointer must have a "-> *" or ". *" call.