G
N
I
D
A
O
L

回调函数最简单用法

回调函数最简单用法

#include <iostream>
using namespace std;


int toZeroDown(int n, void *contex) {
	cout  << "toZeroDown:" << -*(int*)(contex) << endl;
	return 0;
}

int toZeroUp(int n, void *contex) {
	cout << "toZeroUp:" << *(int*)(contex) << endl;
	return 0;
}

typedef int(*CallBackFunc)(int n, void *contex);

void registNumCallBack(CallBackFunc callback, void *contex)
{
	int n = 3;
	callback(n, contex);
}

int main()
{
	for (int i = 0; i < 10; i++)
	{
		if (i % 2) {
			registNumCallBack(toZeroDown, &i);
		}
		else
		{
			registNumCallBack(toZeroUp, &i);
		}
	}
	
}
posted @ 2022-01-15 11:56  StimuMing  阅读(42)  评论(0编辑  收藏  举报