2012.1.10 回调函数
只要把你的函数写好并把地址传过去就行,至于如何调用你的函数,那你不需要管,调用部分的代码不是你写。
#include <iostream>
using namespace std;
typedef void (*CALLBACK)(int a,int b);
class base
{
private:
int m;
int n;
static CALLBACK func;
public:
void registercallback(CALLBACK fun,int k,int j);
void callcallback();
};
CALLBACK base::func=NULL;
void base::registercallback(CALLBACK fun,int k,int j)
{
func=fun;
m=k;
n=j;
}
void base::callcallback()
{
base::func(m,n);
}
void seiya(int a,int b)
{
cout<<a<<endl<<b<<endl;
cout<<"this is seiya callback function"<<endl;
}
void zilong(int a,int b)
{
cout<<a<<endl<<b<<endl;
cout<<"this is zilong callback function"<<endl;
}
void main(void)
{
base ba;
ba.registercallback(seiya,2,3);
ba.callcallback();
ba.registercallback(zilong,5,6);
ba.callcallback();
}