簡體   English   中英

c++:可以使用 boost::bind 將成員 function 轉換為預期的 function 指針簽名嗎?

[英]c++: Can boost::bind be used to convert member function to expected function pointer signature?

我正在使用將 function 指針傳遞給方法調用的第 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 方法成為 class 的成員 function。 通常我使用 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 *)'

我嘗試為 function 的 arguments 添加占位符,但這沒有任何區別。 我正在嘗試做的事情可能嗎? 有沒有辦法投射綁定結果?

謝謝!

不是開箱即用。 但是,讓我勾勒一下你如何做到這一點。

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當然會更容易

boost::bind產生一個 function object ,這與指向函數的指針完全不同。 它是一個帶有重載operator()的 object 。

我會說它不能在這里使用。

你不能那樣做。 bind庫創建的是function對象,不是真正的函數,生成的指針的類型會有所不同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM