Algorithm by C/C++ : Callback function


1. general description

Developers are often confused  by what  a callback function is because of the name of the thing.
A callback function is a function which is:
①. passed as an argument to another function
②. invoked after some kind of event.
Once its parent function completes, the function passed as an argument is then called.



The so-called callback, is the module A through a function B() to complete a certain function, but the function a() is the callback function.

The definition of the callback function pointer is defined in module B, and the registration function is also implemented.


2. Demo and Codes

#include <stdio.h>  
typedef void (*CallBackFun)(int p); 
typedef void (*CallBackMessage)(void); 
void PrintNum(int n);  
void ShowNum(int n,CallBackFun x);   
void ShowMessage(CallBackMessage x);
void PrintMessage1();  
void PrintMessage2();  
void PrintMessage3();  

  
int main(){  
   ShowNum(11111,PrintNum);  
   ShowNum(22222,PrintNum);  
   ShowMessage(PrintMessage1);  
   ShowMessage(PrintMessage2);  
   ShowMessage(PrintMessage3);  
}  
  
void PrintNum(int n){  
   printf("Test1 is called,the number is %d\n",n);  
}  
  
void ShowNum(int n,CallBackFun x)
{  
   x(n);
}  
  
 
void PrintMessage1(){  
   printf("This is the message 1!\n");  
}  
  
void PrintMessage2(){  
   printf("This is the message 2!\n");  
}  
  
void PrintMessage3(){  
   printf("This is the message 3!\n");  
}  

void ShowMessage(CallBackMessage x)
{  
    x();
}  

Author:Kyrie




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值