1

我正在使用将函数指针传递给方法调用的第 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 *)'

我尝试为函数的参数添加占位符,但这没有任何区别。我正在尝试做的事情可能吗?有没有办法投射绑定结果?

谢谢!

4

3 回答 3

3

不是开箱即用。但是,让我勾勒一下你如何做到这一点。

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();
  }
};

如果你能从中得到你的当然更RtspClientSessionManager容易RtspClient

于 2011-07-15T15:13:46.280 回答
2

boost::bind生成一个函数对象,它与指向函数的指针完全不同。它是一个重载的对象operator()

我会说它不能在这里使用。

于 2011-07-15T14:56:00.490 回答
1

你不能那样做。该bind库创建函数对象,而不是真正的函数,并且生成的指针的类型会有所不同。

于 2011-07-15T14:56:38.180 回答