我使用的是第三方库,它将函数指针传递给方法调用。
class RTSPClient{
public:
...
typedef void (responseHandler)(RTSPClient* rtspClient,
int resultCode, char* resultString);
...
unsigned sendOptionsCommand(responseHandler* responseHandler,
Authenticator* authenticator = NULL);
};正常用法如下:
void continueAfterOPTIONS(RTSPClient* client,
int resultCode, char* resultString);
....
RTSPClient* pClient;
....
pClient->sendOptionsCommand(continueAfterOPTIONS, NULL);现在我想让continueAfterOPTIONS方法成为一个类的成员函数。我通常使用boost::bind来做这件事:
pRtspClient>sendOptionsCommand(boost::bind(&RtspClientSessionManager::continueAfterOPTIONS, this), NULL);结果是
error C2664: 'RTSPClient::sendOptionsCommand' : cannot convert parameter 1 from 'boost::_bi::bind_t<R,F,L>' to 'RTSPClient::responseHandler (__cdecl *)'我尝试在函数的参数中添加占位符,但没有什么不同。我想要做的事情是可能的吗?有可能有一种方法来转换绑定结果吗?
谢谢!
发布于 2011-07-15 23:13:46
不是开箱即用的。然而,让我来概述一下你是如何做到这一点的。
struct Foo : RTSPClient {
boost::function<void(int resultCode, char* resultString)> bound;
Foo(boost::function<void(int resultCode, char* resultString)> bound) : bound(bound) {}
// Need a static method to get a regaulr function pointer
static void CallBack(RTSPClient* _this, int resultCode, char* resultString) {
_this->CallBack(int resultCode, char* resultString
void CallBack(int resultCode, char* resultString) {
bound();
}
};当然,如果您可以从RtspClient派生RtspClientSessionManager,就会更容易
发布于 2011-07-15 22:56:00
boost::bind生成一个函数对象,它与指向函数的指针完全不同。它是一个重载了operator()的对象。
我想说它不能在这里使用。
发布于 2011-07-15 22:56:38
你不能那样做。bind库创建的是函数对象,而不是真正的函数,生成的指针类型会有所不同。
https://stackoverflow.com/questions/6708904
复制相似问题