回调函数的定义:
在网上大致找到以下几种对于回调函数的描述:
- 回调函数就是一个被作为参数传递的函数
- 回调函数就是一个通过函数指针调用的函数。如果把函数的地址作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们说这是回调函数。
- A callback is any function that is called by another function which takes the first function as a parameter
- A callback is a function that is passed as an argument to another function and is executed after its parent function has completed
回调函数的形式:
#include <iostream>
using namespace std;
typedef int(*CallBackFunc)(int,int);
int call_func(int num1,int num2,CallBackFunc func){
return func(num1,num2);
}
int operation_plus(int num1,int num2){
return num1+num2;
}
int operation_minus(int num1,int num2){
return num1-num2;
}
int main(int argc,char *args[]){
int a=1;
int b=2;
cout<<a<<" + "<<b<<" = "<
|