This article mainly introduces C + + function pointers and callback function examples, need friends can refer to the following
1. Function pointer
function pointer is a pointer, just this pointer it is not like a normal pointer to be a variable, at this point it is pointing to a function, that is, it stores the address of a function, if we change its value, let it point to the address from point Funa to Point Funb, Then the function of the pointer changes.
2. Callback function
What is a callback function? The callback function is actually a function called by the function pointer! If you pass a pointer to a function as a parameter to a B function, and then the pointer in B functions through a function, call a function, this is the callback mechanism. The B function is the callback function.
3. Use of function pointers
3.1 Function Pointer Declaration
typedef return Type (* function pointer type name) (functional parameter list);
The code is as follows:
#include
using namespace Std;
typedef void (*fun) (int,int); Defining function pointer types
void min (int a,int b);
void Max (int a,int b);
void min (int a,int b)
{int Minvalue=a
std::cout<< "min value is" <
}
void Max (int a,int b)
{int maxvalue=a>b?a:b;
std::cout<< "Max value is" <
}
int main ()
{Fun pfun=null;//define function pointer variable Pfun
Pfun=min; Both of the assignment methods are supported
pfun=&min;
Pfun (1,2); This gets the minimum value
Pfun=max;
pfun=&max;
Pfun (1,2); Get maximum value here
return 0;
}
Note : Please pay attention to the triple programming Tutorials section for more wonderful articles .