c++:可以使用boost::bind将成员函数转换为预期的函数指针签名吗?

标签 c++ boost-bind pointer-to-member

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

相关文章:

c++ - 基于范围的 vector for 循环

c++ - c++中的时间计数器

c++ - boost::bind 返回的仿函数是否只有绑定(bind)参数等同于不带参数的函数?

c++ - 在 boost::bind 中使用 boost 信号

c++ - 传递和转换方法指针

c++ - 如何使用成员函数指针调用函数

C++使用数组中的类执行函数

c++ - 各种小部件中 Qt 应用程序超链接中的 Cursor apper odd

c++ - 如何测试 "binary"或 unset char*?

c++ - 将 boost::bind 与包含 boost::mutex 的类一起使用