关于回调函数 理解一下

typedef void(*CallbackFun)(int);   //void类型的函数指针   
void printLove(int len)        //本身的实现函数要和函数指针形式相一致   
{  
cout<<"Love u- "<<len<<endl;  
}  

void printFuck(int len)  
{  
cout<<"Fu<l< u- "<<len<<endl; 
}  

void testfun_API(int times, CallbackFun pFun)  //模拟API函数或DLL函数   
{  
int i;  
for (i = 0; i < times; ++i)  
{  
pFun(i);  
}  
cout<<"Love or Fu<l< ?"<<endl; 
}  

int main(void)
{
testfun_API(3, printLove); //call API   
testfun_API(3, printFuck);  
printLove(2);  
}

运行结果函数

Love u- 0
Love u- 1
Love u- 2
Love or Fu<l< ?
Fu<l< u- 0
Fu<l< u- 1
Fu<l< u- 2
Love or Fu<l< ?
Love u- 2
请按任意键继续. . .
相关文章
相关标签/搜索