回调函数另解

 


#include <iostream>
#include <string>
using namespace std;


typedef void(*pcb1)(char *);
typedef void(*pcb2)(long value);
typedef void(*pcb3)(string str);

struct Funtion
{
 pcb1 f1;
 pcb2 f2;
 pcb3 f3;

}myFun;

void GetCallBack(Funtion fun)//(pcb1 callback)
{
 /*do something*/
 char buffer[] = "hello world!";
 //callback(buffer);
 fun.f1(buffer);
}

void CallbackFun1(char *s)
{
 /* do something */
 cout<<s<<endl;
 cout<<"call CallbackFun1!"<<endl;
}

void CallbackFun2(char *s)
{
 /* do something */
 cout<<s<<endl;
 cout<<"call CallbackFun2!"<<endl;
}

void CallbackFun3(char *s)
{
 /* do something */
 cout<<s<<endl;
 cout<<"call CallbackFun3!"<<endl;
}

pcb1 funArray[3] = {CallbackFun1,CallbackFun2,CallbackFun3};

 


int _tmain(int argc, _TCHAR* argv[])
{
 Funtion myFuntion;

 for(int i=0;i<3;i++)
 {
  myFuntion.f1 = funArray[i];
  GetCallBack(myFuntion);
 }

 return 0;
}

 

你可能感兴趣的:(回调函数另解)