1

関数ポインタをメソッド呼び出しに渡すサードパーティ ライブラリを使用しています。

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 に答える