当前位置: 首页 > 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是绑定特定的函数(具体的要调用的函数)。
http://www.xdnf.cn/news/323731.html

相关文章:

  • C++回顾 Day5
  • VRM Add-on for Blender 学习笔记
  • 如何测试 esp-webrtc-solution_solutions_doorbell_demo 例程?
  • C++ 继承
  • 文章记单词 | 第68篇(六级)
  • Synthetic Data Kit:LLM微调的语料提炼方案
  • iPhone 和 Android 在日期格式方面的区别
  • 前端日常 · 移动端网页调试
  • c++混淆工具Hikari-LLVM15-llvm-18.1.8rel编译安装
  • Android 蓝牙开发调试总结
  • 多模态学习(一)——从 Image-Text Pair 到 Instruction-Following 格式
  • Java学习手册:数据库事务相关知识
  • 护照阅读器简介
  • 算法导论第7章思考题
  • 16.Three.js 中的 RectAreaLight 全面详解 + Vue 3 实战案例
  • 动态规划之01背包——三道题助你理解01背包
  • 深入浅出之FPN (Feature Pyramid Networks for Object Detection)
  • vue3 element-plus 输入框回车跳转页面问题处理
  • 拒绝服务攻击(DoS/DDoS/DRDoS)详解:洪水猛兽的防御之道
  • 嵌入式学习--江协51单片机day2
  • 基于英特尔 RealSense D455 结构光相机实现裂缝尺寸以及深度测量
  • STM32基础教程——硬件SPI
  • OpenMVS 的编译与运行
  • 2025年链游行业DDoS与CC攻击防御全解析:高带宽时代的攻防博弈
  • 算法-时间复杂度和空间复杂度
  • 【Python 函数】
  • 【c++】 我的世界
  • 【EasyPan】saveShare代码分析
  • 部署Prometheus+Grafana简介、监控及设置告警(一)
  • ChromeDriverManager的具体用法