C++11 成员函数作为回调函数使用示例

前言

std::bind()被广泛地应用在新式的回调函数中。
C++11以前类的普通成员函数不能作为回调函数去注册,因为将普通成员函数注册给对方,但对方使用这个函数指针时,就会发生参数列表匹配的问题,因为少了隐含的this
静态成员函数不包含this指针,所以一般将静态成员函数注册给对方。

C++11推出std::bind()和std::function搭配,前者生成新的调用对象,参数个数可以小于绑定函数的参数个数,少的参数,按位占用。后者保存函数调用类型的函数对象,使用该对象进行设置参数即可。

示例1

先看一个例子来热热身,熟悉一下std::bind和std::function

#include <functional> //所需std::bind和std::function头文件
#include <iostream>
#include <map>

using namespace std;
// 使用std::bind时记得和::bind区别开,就怕作用于污染,误用::bind

//除法运算
class Division {
public:
	int operator()(int i, int j) { return i / j; }
};

//乘法运算
int Multiplication(int i, int j) { return i * j; }

//减法运算
int Substraction(int i, int j) { return i - j; }

//回调注册函数
int CallbackReg(function<int(int, int)> &func, int i, int j) { return func(i, j); }

//回调注册函数1,
int CallbackReq1(function<int(int)> &&func, int i) { return func(i); }

int main() {

	// 此function接受函数调用类型为int(int, int)的调用对象
	function<int(int, int)> func1 = [](int i, int j) { return i + j; }; //lambda
	function<int(int, int)> func2 = &Substraction;						//函数指针
	function<int(int, int)> func3 = Multiplication;						//函数名
	function<int(int, int)> func4 = Division();							//重载调用运算符的对象

	//可将function类型存在容器中,来一次映射
	map<int, function<int(int, int)>> mpFuncs;
	mpFuncs[1] = func1;
	mpFuncs[2] = func2;
	mpFuncs[3] = func3;
	mpFuncs[4] = func4;

	//这里做着玩,映射一个数字和字符串
	map<int, string> mpOprs{{1, " + "}, {2, " - "}, {3, " * "}, {4, " / "}};

	// 便利map调用容器内函数对象们
	for (auto& it : mpFuncs) {
		cout << "calculator :" << 20 << mpOprs[it.first] << 5 
			<< " = " << CallbackReg(it.second, 20, 5) << endl;
	}

	//使用std::bind,产生一个新的调用对象bindFunc(int i), 200作为int Multiplication(int i, 200)
	//std::placeholders 有个N个占位符(vs此版为20个):_N,表示占用绑定函数的第n个位子
	int pre = 300;
	auto bindFunc = std::bind(Multiplication, placeholders::_1, pre);
	cout << bindFunc(3) << endl;

	//bind最重要的一点在于参数绑定,如下例注册回调,参数就从2个变成了1个
	cout << CallbackReq1(std::bind(Multiplication, placeholders::_1, 200), 2) << endl;

	return 0;

}

calculator :20 + 5 = 25
calculator :20 - 5 = 15
calculator :20 * 5 = 100
calculator :20 / 5 = 4
900
400

好,在了解了std::bindstd::function之后来看一个平时常遇到的C++式的回调函数注册

示例2:

#include <functional>
#include <iostream>
#include <string>
#include <memory>

using namespace std;
using namespace std::placeholders; //占位符_N所在的命名空间

using CallBackFuncType = function<void(string const&)>; 

class Client {
public:
	string name;
	CallBackFuncType serverFunc;

	Client() :name("Vergo"), serverFunc(nullptr) {}
	~Client() {}

	void SetCallBack(const CallBackFuncType &func) { serverFunc = func; }
	void DoCallBack() { serverFunc(name); }
};

class Server {
public:
	Client *m_clt;
	Server() : m_clt(nullptr) { m_clt = new Client; }
	~Server() { if (m_clt) delete m_clt; m_clt = nullptr; }

	//回调函数本数
	void MyCallBackFunc(string const& str) { cout << "The name of client is " << str << endl; }
	//注册回调,将this指针绑定到回调函数中
	void RegCallBackFunc() { if (!m_clt) return;  m_clt->SetCallBack(CallBackFuncType(std::bind(&Server::MyCallBackFunc, this, _1))); }
	//回调
	void GiveMeCallBack() { if (!m_clt) return; m_clt->DoCallBack(); }
};


int main() {

	Server testClass;
	testClass.RegCallBackFunc();
	testClass.GiveMeCallBack();	

	return 0;
}
The name of client is Vergo
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如qsort 等函数需要函数指针才能回调 用此函数库可以将成员函数指针转为普通函数指针 测试代码如下 #include <stdio.h> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <math.h> using cmpfunc = int(__cdecl*)(const void*, const void*); using DebugArrayFunc = void(__stdcall *)(std::string &out;); #include "thunk.h" class MySort { public: int Rule; MySort(int a):Rule(a){} // 回调函数 template<typename T> int __cdecl sort(const void* a, const void* b); }; class Test { public: std::vector<int> mm; void Sort(int (*comp)(const void *,const void *)) { return qsort(mm._Myfirst,mm.size(),sizeof(int),comp); } void Entry(DebugArrayFunc func) { std::string string; cmpfunc comp; TemplateThunk athunk; // 正序 comp = (cmpfunc)athunk.GetCall(&MySort;::sort<int>, &MySort;(0)); Sort(comp); func(string); std::cout << string << std::endl; // 逆序 comp = (cmpfunc)athunk.GetCall(&MySort;::sort<int>, &MySort;(1)); Sort(comp); func(string); std::cout << string << std::endl; } }; class CallBack { public: std::vector<int> *pthis; CallBack(std::vector<int> *ff):pthis(ff){} void __stdcall DebugArray(std::string &out;) { char buff[100]; char *aa = buff; for each (auto a in *pthis) { aa += sprintf(aa, "%d ", a); } out.assign(buff); } }; void main() { TemplateThunk athunk; Test tt; tt.mm = { 1, 3, 7, 8, 5, 6, 4, 2, 3, 10 }; tt.Entry(athunk.GetCall(&CallBack;::DebugArray,&CallBack;(&tt;.mm))); } template <typename T> int __cdecl MySort::sort(const void* a, const void* b) { return Rule ? *static_cast<const T*>(a)-*static_cast<const T*>(b) : *static_cast<const T*>(b)-*static_cast<const T*>(a); }
### 回答1: 在 C 语言中,注册回调函数通常是通过函数指针实现的。回调函数是在特定事件发生后被调用的函数,它会执行一些特定的逻辑。为了注册回调函数,我们需要定义一个指向函数的指针,并将它作为参数传递给需要调用回调函数的函数中。 以下是一个简单的示例,演示如何在 C 语言中注册回调函数: ```c #include <stdio.h> // 定义回调函数 void callback_func() { printf("Callback function is called.\n"); } // 注册回调函数 void register_callback(void (*callback)(void)) { // 假设在某个事件发生后需要调用回调函数 // 这里模拟事件发生并手动调用回调函数 printf("Event occurs.\n"); (*callback)(); } int main() { // 传递回调函数的函数指针 register_callback(&callback_func); return 0; } ``` 在上面的示例代码中,我们首先定义了一个回调函数`callback_func()`,然后定义了一个`register_callback()`函数,它接收一个函数指针参数,并在函数体中调用它。 在`main()`函数中,我们通过`register_callback()`函数向其中传递回调函数的函数指针,以在事件发生时调用该函数。 以上是 C 语言注册回调函数的一个简单示例,实际应用中可能还需要进行更多的相关处理和逻辑设计。 ### 回答2: 在C语言中,注册回调函数Callback Function)是指将一个函数指针作为参数,在函数中调用该指针所指向的函数。这里的指针通常被称为回调函数指针,而被调用的函数被称为回调函数回调函数的主要作用是让程序能够在特定情况下调用该函数,以执行适当的操作。 在实际应用中,注册回调函数通常采用函数指针的方式实现。以下是C语言中注册回调函数的基本步骤: 1.定义回调函数指针类型 首先需要定义一个回调函数指针类型(Callback Function Pointer Type),以便后面注册回调函数使用。例如: typedef void (*CallbackFuncType)(int, char*); 其中,void表示该回调函数无返回值,*表示该函数指针变量指向的是地址,CallbackFuncType是我们自己定义的类型名称,括号中的int和char*表示该回调函数的多个参数类型。 2.定义回调函数 接着需要定义回调函数,在该函数中执行需要的操作。例如: void CallbackFunc(int a, char* b) { printf("a=%d, b=%s\n", a, b); } 该函数的作用是打印接收到的参数a和b的值。 3.注册回调函数 最后,需要进行注册操作,将回调函数作为参数传递给需要调用该函数的主函数。例如: void RegisterCallback(CallbackFuncType Callback) { int a = 10; char b[] = "Hello"; // do something... Callback(a, b); } 该函数接收参数为一个回调函数指针变量Callback,并在函数内部调用Callback函数。在主函数中,可以通过传递一个合适的回调函数指针来实现在需要的情况下调用回调函数。 总之,在C语言中,注册回调函数是一种非常方便、高效的编程方式,可以帮助程序更好地满足用户需求,同时也为程序的灵活性、可扩展性提供了很好的支持。 ### 回答3: 在C语言中,注册回调函数通常是将函数指针作为参数传递给另一个函数,以便在需要时调用它。这种技术被广泛应用于事件处理和回调机制。 要注册回调函数,需要先定义回调函数的类型,然后将其作为参数传递给需要调用回调函数的函数。例如,考虑以下代码片段: ``` #include <stdio.h> typedef void (*callback_t)(int); void register_callback(callback_t callback) { printf("注册回调函数\n"); // 在适当的时候调用回调函数 callback(42); } void callback(int value) { printf("回调函数被调用,传递的值为:%d\n", value); } int main() { // 注册回调函数 register_callback(callback); return 0; } ``` 在这个例子中,我们首先定义了一个回调函数类型`callback_t`,它接受一个int类型的参数并返回void。然后,我们定义了一个函数`register_callback`,它接受一个指向回调函数的指针,并在需要时调用它。最后,我们定义了一个回调函数`callback`,它简单地打印传递给它的值。 在主函数中,我们将函数`callback`的地址作为参数传递给`register_callback`函数,从而注册回调函数。当函数`register_callback`需要调用回调函数时,它将传递一个值42作为参数,并执行代码打印出传递的值。 通过这种方式,我们可以动态地将回调函数注册到其他函数中,并在需要时调用它们。这为事件处理和异步编程等情况提供了非常有用的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

歪锅锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值