首先,回调函数的概念需要了解一下。

何谓回调函数? 在我个人理解,回调函数指的是在调用某个函数的时候,把自己的函数地址作为调用函数的其中一个参数。

接下来通过示例代码来解释一下:

//回调函数Demo
#include <iostream>
#include <stdio.h>
#include "windows.h"

using namespace std;
typedef int(*demoFunc)(int,int); //void类型的函数指针,demoFunc是一种回调函数

int add(int a,int b) //自己的实现函数要和函数指针形式相一致
{
return a + b;
}

int minus_time(int a, int b) //自己的实现函数要和函数指针形式相一致
{
return a - b;
}

//测试回调函数是否成功
void testCallBack(int counts, demoFunc func)
{
for (int i= 0; i < counts; i++ )
{
int result = func(i, i +1);
cout << result << endl;
}
}


int main()
{
//测试是否成功
testCallBack(5, add);
testCallBack(6, minus_time);
}

其中demoFunc指的是指向返回值为int类型的函数参数;

其中函数参数也为int,int