回调函数
1.什么样的函数可以做回调函数
全局函数,静态函数
2.C中的回调函数
Demo1(直接在网上找的https://blog.csdn.net/EbowTang/article/details/43065277)
// ConsoleAppBrightCon.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include "windows.h"
typedef void(*CallbackFun)(int); //void类型的函数指针,CallbackFun被声明成了一种类型,如同int char一样
void printWelcome(int len) //自己的实现函数要和函数指针形式相一致
{
printf("WelcomeWelcome -- %d\n", len);
}
void printGoodbye(int len)
{
printf("GoodbyeGoodbye -- %d\n", len);
}
void testfun_API(int times, CallbackFun pFun) //模拟API函数或DLL函数
{
for (int i = 0; i < times; ++i)
pFun(i);
printf("Goodbye??????Welcome??????\n");
}
void main(void)
{
testfun_API(10, printWelcome); //调用 API
testfun_API(10, printGoodbye);
printWelcome(5);
system("pause");
}
Demo2
#include <stdio.h>
typedef int(*callback)(int,int);
int add(int a,int b,callback p){
return (*p)(a,b);
}
int add(int a,int b){
return a+b;
}
int main(int argc,char *args[]){
int res = add(4,2,add);
printf("%d\n",res);
return 0;
}
---------------------
作者:一度凡尘
来源:CSDN
原文:https://blog.csdn.net/yidu_fanchen/article/details/80513359
版权声明:本文为博主原创文章,转载请附上博文链接!
Demo3
#include <stdio.h>
typedef int (callBack)(const void *buffer,size_t size,char *p_out);
void callFunc(callBack *consume_bytes, char *p_out) {
printf("callFunc\n");
const void *buffer = NULL;
consume_bytes(buffer,0,p_out); //传入值可以随便填
}
int callBackFunc(const void *buffer, size_t size, char *p_out){
printf("callBackFunc\n");
memset(p_out,0x00,sizeof(char)*100);
strcpy(p_out,"encoderCallback:this is string.");
return 1;
}
int main(int argc,char *args[]){
char p_out[100];
callFunc(callBackFunc,p_out);
printf("%s\n",p_out);
return 0;
}
---------------------
作者:一度凡尘
来源:CSDN
原文:https://blog.csdn.net/yidu_fanchen/article/details/80513359
版权声明:本文为博主原创文章,转载请附上博文链接!
3.C++中的回调函数
3.1从C中继承过来(https://www.cnblogs.com/WushiShengFei/p/9232756.html)
3.2利用function和bind来实现回调的功能(https://www.cnblogs.com/WushiShengFei/p/9233470.html)
其它参考
https://www.cnblogs.com/hualalasummer/p/3712179.html(从多方面理解回调函数)
https://www.jb51.net/article/59766.htm(回调函数+静态指针)
https://www.jb51.net/article/116321.htm(回调函数调用关系图)
http://www.cnblogs.com/ioleon13/archive/2010/03/02/1676621.html(应用场景)
http://www.360doc.com/content/13/0601/11/6295074_289655195.shtml(C++ 回调函数定义格式)
https://blog.csdn.net/hyp1977/article/details/51784520(function和bind来实现回调的功能)

浙公网安备 33010602011771号