我正在使用将函数指针传递给方法调用的第 3 方库。
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 *)'
我尝试为函数的参数添加占位符,但没有任何区别。我正在尝试做的事情可能吗?有没有办法转换绑定(bind)结果?
谢谢!
最佳答案
不是开箱即用的。不过,让我简要说明一下您可以如何做到这一点。
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
,当然会更容易
关于c++:可以使用boost::bind将成员函数转换为预期的函数指针签名吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6708904/