在回调函数中,经常需要将函数的指针类型作为参数传入到回调函数中,在回调函数中执行指针函数。
指针函数的组成如下图所示:
当typedef去掉时,pFunc就是一个函数指针变量。
常规的定义方法如下:
#include <string> using namespace std; //函数指针类型定义 typedef int (*pFunc)(int, int); int add(int a, int b) { return a+b; } int main(int argc, char *argv[]) { pFunc pAdd = add; cout << pAdd(3, 5) << endl; return 0; }
也可以如下定义:
#include <string> using namespace std;
//重定义一个函数原型 typedef int func(int, int); //重定向函数原型指针为函数指针类型 typedef func* pFunc; int add(int a, int b) { return a+b; }
int main(int argc, char *argv[])
{
pFunc pAdd = add;
cout << pAdd(3, 5) << endl;
return 0;
}
原文:https://www.cnblogs.com/lwp-boy/p/13173756.html