跨类回调函数的两种实现方式
问题背景:在网络编程中客户端收到服务器的数据往往需要回调到上层进行处理,这里就涉及到跨类回调函数的问题.
简单模拟实现为:有两个类A 和 B, B收到数据后需要回调A中的接口。
1. C++11后的实现方式
使用bind和function组合实现
/*********************************************************************************************
* @Copyright (c) , All rights reserved.
* @file: AClass.hpp
* @version: ver 1.0
* @author: zbb
* @brief:
* @change:
* @email: binbin_erices@163.com
* Date Version Changed By Changes
* 2020/3/25 0:17 1.0 zbb create
**********************************************************************************************/
#include <iostream>
#include "BClass.hpp"
#include <functional>
using namespace std;
class AClass
{
public:
AClass(BClass *pb)
: m_ia(0)
, m_ib(-1)
, m_c(pb)
{
if (pb)
{
pb->setCallBack(std::bind(&AClass::funCall, this, std::placeholders::_1, std::placeholders::_2));
}
}
~AClass() {}
void funCall(int a, int b)
{
m_ia = a;
m_ib = b;
cout << "---a " << m_ia << m_ib << endl;
}
private:
int m_ia;
int m_ib;
BClass *m_c;
};
/*********************************************************************************************
* @Copyright (c) , All rights reserved.
* @file: BClass.hpp
* @version: ver 1.0
* @author: zbb
* @brief:
* @change:
* @email: binbin_erices@163.com
* Date Version Changed By Changes
* 2020/3/25 0:18 1.0 zbb create
**********************************************************************************************/
#include <iostream>
#include <functional>
using namespace std;
class BClass
{
typedef std::function<void(int, int)> callBack;
public:
BClass() {}
~BClass() {}
void SetValue()
{
if (m_fun)
{
m_fun(10, 20);
}
}
void setCallBack(const callBack& cb)
{
m_fun = cb;
}
private:
callBack m_fun;
};
/*********************************************************************************************
* @Copyright (c) , All rights reserved.
* @file: main.cpp
* @version: ver 1.0
* @author: zbb
* @brief:
* @change:
* @email: binbin_erices@163.com
* Date Version Changed By Changes
* 2020/3/25 0:18 1.0 zbb create
**********************************************************************************************/
#include "AClass.hpp"
int main()
{
BClass b;
AClass a(&b);
b.SetValue();
return 0;
}
2. C++11前的实现方式
对于类的静态函数实现比较简单,着重说一下类的非静态函数。实现方式和静态函数类似,在传递回调函数的时候使用静态函数进行包装,把this指针进行传递。具体代码如下:
/*********************************************************************************************
* @Copyright (c) , All rights reserved.
* @file: AClass.hpp
* @version: ver 1.0
* @author: zbb
* @brief:
* @change:
* @email: binbin_erices@163.com
* Date Version Changed By Changes
* 2020/3/25 0:17 1.0 zbb create
**********************************************************************************************/
#include <iostream>
#include "BClass.hpp"
using namespace std;
class AClass
{
public:
AClass(BClass *pb)
: m_ia(0)
, m_ib(-1)
, m_c(pb)
{
if (pb)
{
pb->setCallBack(this, WrapFunc);
}
}
~AClass() {}
static void WrapFunc(void *obj, int a, int b)
{
AClass *p = static_cast<AClass*>(obj);
if (p)
{
p->funCall(a, b);
}
}
void funCall(int a, int b)
{
m_ia = a;
m_ib = b;
cout << "---a " << m_ia << m_ib << endl;
}
private:
int m_ia;
int m_ib;
BClass *m_c;
};
/*********************************************************************************************
* @Copyright (c) , All rights reserved.
* @file: BClass.hpp
* @version: ver 1.0
* @author: zbb
* @brief:
* @change:
* @email: binbin_erices@163.com
* Date Version Changed By Changes
* 2020/3/25 0:18 1.0 zbb create
**********************************************************************************************/
#include <iostream>
using namespace std;
class BClass
{
//typedef std::function<void(int, int)> callBack;
typedef void(*callBack)(void*, int, int);
public:
BClass() {}
~BClass() {}
void SetValue()
{
if (m_fun)
{
m_fun(m_obj, 10, 20);
}
}
void setCallBack(void *obj, const callBack& cb)
{
m_obj = obj;
m_fun = cb;
}
private:
callBack m_fun;
void *m_obj;
};
/*********************************************************************************************
* @Copyright (c) , All rights reserved.
* @file: main.cpp
* @version: ver 1.0
* @author: zbb
* @brief:
* @change:
* @email: binbin_erices@163.com
* Date Version Changed By Changes
* 2020/3/25 0:18 1.0 zbb create
**********************************************************************************************/
#include "AClass.hpp"
int main()
{
BClass b;
AClass a(&b);
b.SetValue();
return 0;
}