0

この質問は、私の別の質問に関連しています-boost :: bindは、ポインタを必要とする関数の引数である関数オブジェクトを返します

のインターフェースを除く

bridge_set_pound_var_func

変更することはできません。

また、boost::functionまたはboost::bind大規模なプロジェクトではうまく機能しません。

私の新しいコードは次のとおりです。

#include <iostream>

class myA
{
 public:

 int bridge_set_pound_var_func(int (*fp)(const char *, char *, void *), void *arg)
 {
     void * b = NULL;
     int a = fp("this is poundVar", "ths is t1", b) ;
     std::cout << "bridge_set_pound_var_func is called "<< " , a is " << a << std::endl ;
     return 0;

 }

};

class myC
{
public:
myA *myOA;
int func(const char * poundVar , char * t1, void * t2);

int myCCall()
{
    myA myAO;
    myOA = &myAO;
    std::cout << "myCCall is called " << std::endl;

    myOA->bridge_set_pound_var_func( &myC::func, (void *)this );

    return 0;

 }

};

int myC::func(const char * poundVar , char * t1, void * t2)
{
 std::cout << "myC::func is called " << std::endl;
 return 1;

}

int main()
{
  myC myCO ;
  myC *m1p = &myCO ;
  m1p->myCCall() ;

  return 0 ;
}

// EOF

Linuxでのエラー:

  In member function 'int myC::myCCall()':

  error: no matching function for call to 'myA::bridge_set_pound_var_func(int (myC::*)(const char*, char*, void*), void*)'

  candidates are: int myA::bridge_set_pound_var_func(int (*)(const char*, char*, void*), void*)

VMSのエラー:

 In member function 'int myC::myCCall()':

 error: no matching function for call to 'myA::bridge_set_pound_var_func(int (myC::*)(const char*, char*, void*), void*)'

 candidates are: int myA::bridge_set_pound_var_func(int (*)(const char*, char*, void*), void*)
4

1 に答える 1

1

簡単に言うと、メンバー関数へのポインターは関数へのポインターではありません。前者は彼らが呼ばれているオブジェクトについて知る必要がありますが、後者は知りません。使用される一般的なアプローチは、通常存在する「ユーザーデータ」を使用void*して適切な基本クラスをポイントし、ポインターをキャストして、対応する仮想関数を呼び出すことです。そこで、必要なオブジェクトコンテキストを簡単に復元できます。

于 2012-09-15T21:19:27.430 に答える