回调函数最简单用法
// CallBackFunc.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
using namespace std;
string toZeroDown(int n, void *contex) {
cout << "toZeroDown:" << -*(int*)(contex) << endl;
return "toZeroDown";
}
string toZeroUp(int n, void *contex) {
cout << "toZeroUp:" << *(int*)(contex) << endl;
return "toZeroUp";
}
typedef string(*CallBackFunc)(int n, void *contex);
void registNumCallBack(CallBackFunc callback, void *contex)
{
int n = 3;
cout << "The Option of 'callback()' is:" << callback(n, contex) << endl;
}
int main()
{
for (int i = 0; i < 10; i++)
{
if (i % 2) {
registNumCallBack(toZeroDown, &i);
}
else
{
registNumCallBack(toZeroUp, &i);
}
}
}
输出:
toZeroUp:0
The Option of 'callback()' is:toZeroUp
toZeroDown:-1
The Option of 'callback()' is:toZeroDown
toZeroUp:2
The Option of 'callback()' is:toZeroUp
toZeroDown:-3
The Option of 'callback()' is:toZeroDown
toZeroUp:4
The Option of 'callback()' is:toZeroUp
toZeroDown:-5
The Option of 'callback()' is:toZeroDown
toZeroUp:6
The Option of 'callback()' is:toZeroUp
toZeroDown:-7
The Option of 'callback()' is:toZeroDown
toZeroUp:8
The Option of 'callback()' is:toZeroUp
toZeroDown:-9
The Option of 'callback()' is:toZeroDown
原文:https://blog.csdn.net/Fuel_Ming/article/details/122950838