当前位置: 首页 > news >正文

C++回调函数学习

C++回调函数学习

  • 遇到问题,要学习C++回调函数

遇到问题,要学习C++回调函数

来吧,直接看代码吧
共有4种方法,每种方法都有标识,对用的屏蔽和打开就可以使用
原文在这里:

#include<iostream>
#include<functional>//*2.1 普通函数作为回调函数*//
//void ProgramA_FunA1() { printf("I'am ProgramA_FunA1 and be called..\n"); }
//void ProgramA_FunA2() { printf("I'am ProgramA_FunA2 and be called..\n"); }
//
//void programB_FunB1(void(*callback)()) 
//{
//	printf("I'am ProgramB_FunB1 and be called..\n");
//	callback();
//}
//
//int main(/*int argc, char **argv*/)
//{
//	ProgramA_FunA1();
//	programB_FunB1(ProgramA_FunA2);
//}*2.2 类的静态函数作为回调函数*//
//class ProgramA
//{
//public:
//	void FunA1() { printf("I'am ProgramA_FunA1 and be called..\n"); }
//
//	static void FunA2() { printf("I'am ProgramA_FunA2 and be called..\n"); }
//};
//
//class ProgramB 
//{
//	public:
//		static void FunB1(void(callback)()) 
//		{
//			printf("I'am ProgramB_FunB1 and be called..\n");
//			callback();
//		}
//};
//
//int main()
//{
//	ProgramA Pa;
//	Pa.FunA1();
//
//	ProgramB Pb;
//	Pb.FunB1(ProgramA::FunA2);
//}//****可以看出,以上两种方式没有什么本质的区别。
//****但这种实现有一个很明显的缺点:static 函数
//****不能访问非static 成员变量或函数,会严重限制回调函数可以实现的功能。***2.3 类的非静态函数作为回调函数*//
//class ProgramA
//{
//public:
//	void FunA1() { printf("I'am ProgramA_FunA1 and be called..\n"); }
//
//	void FunA2() { printf("I'am ProgramA_FunA2 and be called..\n"); }
//};
//
//class ProgramB
//{
//public:
//	void FunB1(void (ProgramA::*callback)(),void *context) 
//	{
//		printf("I'am ProgramB_FunB1 and be called..\n");
//		((ProgramA*)context->*callback)();
//	}
//};
//
//int main()
//{
//	ProgramA Pa;
//	Pa.FunA1();
//
//	ProgramB Pb;
//	Pb.FunB1(&ProgramA::FunA2,&Pa);
//}
/******这种方法可以得到预期的结果,看似完美,但是也存在明显不足。
*******比如在programB中FunB1还使用 programA的类型,也就我预先还要知道
*******回调函数所属的类定义,当programB想独立封装时就不好用了。*///*2.4 这里还有一种方法可以避免这样的问题,可以把非static的回调函数 包装为另一个static函数,这种方式也是一种应用比较广的方法*//
//class ProgramA
//{
//public:
//	void FunA1() { printf("I'am ProgramA_FunA1 and be called..\n"); }
//	void FunA2() { printf("I'am ProgramA_FunA2 and be called..\n"); }
//
//	static void FunA2Wrapper(void *context)
//	{
//		printf("I'am ProgramA.FunA2Wrapper() and be called..\n");
//		((ProgramA*)context)->FunA2();  //此处调用的FunA2()是context的函数, 不是this->FunA2()
//	};
//};
//
//class ProgramB 
//{
//public:
//	void FunB1(void (ProgramA::*callback)(),void *context)
//	{
//		printf("I'am ProgramB_FunB1() and be called..\n");
//		((ProgramA *)context->*callback)();
//	}
//
//	void FunB2(void (* callback)(void*), void* context)
//	{
//		printf("I'am ProgramB.FunB2() and be called..\n");
//		callback(context);
//	}
//};
//
//int main()
//{
//	ProgramA Pa;
//	Pa.FunA1();
//
//	ProgramB Pb;
//	Pb.FunB1(&ProgramA::FunA2,&Pa);
//
//	std:: cout<< "\n" ;
//	Pb.FunB2(ProgramA::FunA2Wrapper,&Pa);
//}
//
这种方法相对于上一种,ProgramB中没有ProgramA的任何信息了,是一种更独立的实现方式。
FunB2()通过调用FunA2Wrapper(),实现间接的对FunA2()的调用。FunA2()可以访问和调用对类内的任何函数和变量。
多了一个wrapper函数,也多了一些灵活性。


上面借助wrapper函数实现回调,虽然很灵活,但是还是不够优秀,比如:
1)多了一个不是太有实际用处的wrapper函数。
2)wrapper中还要对传入的指针进行强制转换。
3)FunB2调用时,不但要指定wrapper函数的地址,还要传入PA的地址。//*3 std::funtion和std::bind的使用*//class ProgramA
{
public:void FunA1() { printf("I'am ProgramA.FunA1 and be called..\n"); }void FunA2() { printf("I'am ProgramA.FunA2 and be called..\n"); }static void FunA3() { printf("I'am ProgramA.FunA3 and be called..\n"); }
};
class ProgramB
{typedef std::function<void()> callbackFun;
public:void FunB1(callbackFun callback) {printf("I'am ProgramB.FunB2() and be called..\n");callback();}
};void normFun() {printf("I'am normFun() and be called..\n");}int main()
{ProgramA Pa;Pa.FunA1();printf("\n");ProgramB Pb;Pb.FunB1(normFun);std::cout << "\n";Pb.FunB1(ProgramA::FunA3);printf("\n");Pb.FunB1(std::bind(&ProgramA::FunA2,&Pa));
}
//*****std::funtion支持直接传入函数地址,或者通过std::bind指定。
//*****简而言之,std::funtion是定义函数类型(输入、输出),std::bind是绑定特定的函数(具体的要调用的函数)。

相关文章:

  • Linux:进程控制1
  • 标量/向量/矩阵/张量/范数详解及其在机器学习中的应用
  • 第8章-1 查询性能优化-优化数据访问
  • 构建安全的机器学习推理API:基于FastAPI的用户认证与管理实战
  • 用R语言+随机森林玩转遥感空间预测-基于R语言机器学习遥感数据处理与模型空间预测技术及实际项目案例分析
  • AI工场全面激活电商创意链
  • 配置Hadoop集群-测试使用
  • 拆解 Prompt 工程:五大场景驱动 DeepSeek 超越 ChatGPT
  • Eclipse 插件开发 5 编辑器
  • 【优化策略】离散化
  • 项目改 pnpm 并使用 Monorepo 发布至 npm 上
  • Python----神经网络(《Going deeper with convolutions》论文解读和GoogLeNet网络)
  • gitignore的相关用法
  • 51单片机快速成长路径
  • Spring MVC中Controller是如何把数据传递给View的?
  • 2025ISCC练武校级赛部分题解WP
  • (1-1)Java的JDK、JRE、JVM三者间的关系
  • 可视化魔法指南
  • 如何检查 Watchtower 是否正常工作及更新未生效的排查方法【日常排错】
  • C语言学习之字符函数和字符串函数
  • 第四轮伊美核谈判将于11日在阿曼举行
  • 聆听百年唐调正声:唐文治王蘧常吟诵传习的背后
  • 读图|展现城市品格,上海城市影像走进南美
  • 人民日报整版调查:中小学春秋假,如何放得好推得开?
  • 国家卫健委:有条件的二级及以上综合医院要开设老年医学科
  • 城管给商户培训英语、政银企合作纾困,上海街镇这样优化营商环境