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是绑定特定的函数(具体的要调用的函数)。