如何通过函数指针进行绑定? [英] How to pass function pointer to bind?
问题描述
我有以下代码:
_createNewObjectlistener = eventDispatcher->addCustomEventListener(Constants::EVENT_CREATE_OBJECT, std::bind(&ObjectPlacementManager::receiveCreateObjectEvent, this, std::placeholders::_1));
_eventListeners.insert(_createNewObjectlistener);
_moveNewObjectlistener = eventDispatcher->addCustomEventListener(Constants::EVENT_MOVE_NEW_OBJECT, std::bind(&ObjectPlacementManager::receiveMoveCurrentGameObjectEvent, this, std::placeholders::_1));
_eventListeners.insert(_moveNewObjectlistener);
.... many more listeners created
每个侦听器的创建代码是 Constant :: EVENT_NAME
,并且正在调用该函数,我正在尝试将其封装为一个函数。
Since the only difference between the creation code for each listener, is the Constant::EVENT_NAME
and the function being called, I am trying to encapsulate it into a function.
绑定
的结果必须为const std :: function< void(EventCustom *)>&
The result of bind
must be of type const std::function<void(EventCustom*)>&
像 ObjectPlacementManager :: receiveMoveCurrentGameObjectEvent
之类的函数都具有相同的签名:
The functions like ObjectPlacementManager::receiveMoveCurrentGameObjectEvent
all have the same signature:
void receiveMoveCurrentGameObjectEvent(EventCustom* event){
....
}
typedef void (*callback_function)(EventCustom*);
EventListenerCustom* createCustomListener(callback_function func, std::string EVENT){
auto eventDispatcher = _dragNode->getEventDispatcher();
auto listener = eventDispatcher->addCustomEventListener(EVENT, std::bind(&func, this, std::placeholders::_1));
_eventListeners.insert(_createNewObjectlistener);
return listener;
}
但是我得到的错误是:
No viable conversion from '__bind<void (**)(cocos2d::EventCustom *), bejoi::ObjectPlacementManager *, const std::__1::placeholders::__ph<1> &>' to 'const std::function<void (EventCustom *)>'
我也尝试过制作一个函数:
I have also tried making a function:
EventListenerCustom* createCustomListener(void* func, std::string EVENT){
auto eventDispatcher = _dragNode->getEventDispatcher();
auto listener = eventDispatcher->addCustomEventListener(EVENT, std::bind(func, this, std::placeholders::_1));
return listener;
}
但是我得到的错误是:
No viable conversion from '__bind<void *&, mynamespace:: ObjectPlacementManager *, const std::__1::placeholders::__ph<1> &>' to 'const std::function<void (EventCustom *)>'
推荐答案
第一个错误是因为您要获取函数指针的地址。因此,您要将指向函数的指针传递给 std :: bind
。
The first error is because you're taking the address of a function pointer. So you're passing a pointer to a pointer to a function to std::bind
.
第二个错误是因为您使用了 void *
并以某种方式希望它能正常工作!
The second error is because you used a void *
and somehow expected that to work!
尝试以下MCVE:
struct Event {};
struct Dispatcher {
void addListener(int, const std::function<void(Event *)> &) {}
};
struct Manager {
void receive(Event *) {}
void addListener(int type, void (Manager::*receiver)(Event *)) {
dis.addListener(type, std::bind(receiver, this, std::placeholders::_1));
}
void test() {
addListener(42, &Manager::receive);
}
Dispatcher dis;
};
这篇关于如何通过函数指针进行绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!