10
class Child;
class Parent
{
public:
  void (*funcPointer)();
  void (*funcPointer2)(Parent* _this);
  void (Child::*funcPointer3)();
};

class Child: public Parent
{
public:
  void TestFunc(){

  }
  void Do(){
    Parent p;
    p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)'
    p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)'
    p.funcPointer3=TestFunc; //this works
    p.funcPointer3=&Child::TestFunc; // this works too.
    p.funcPointer3();    // error, term does not evaluate to a function taking 0 arguments
  }
};

メンバー関数を関数ポインターに渡すにはどうすればよいですか?次に、その関数ポインターを呼び出すにはどうすればよいですか?

4

3 に答える 3

8

できません。静的メソッドへのポインターを渡すか、Parentがオブジェクトへのポインターも受け入れる必要があります。

あなたはそのためのboost::bindboost::functionを見たいかもしれません:

#include <boost/bind.hpp>
#include <boost/function.hpp>
struct Y
{
    void say(void) { std::cout << "hallo!";}

    boost::function<void()> getFunc() { return boost::bind(&Y::say, this); }
};

struct X
{
    //non-boost:
    void (Y::*func)();
    Y* objectY;
    void callFunc() { (objectY->*func)(); }

    //boost version:
    boost::function<void()> f;

};


X x;
Y y;
x.f = boost::bind(&Y::say, boost::ref(y));
x.f = y.getFunc();
x.f();
x.func = &Y::say;
x.objectY = &y; 
x.callFunc();
于 2010-11-28T10:10:12.033 に答える
3

最後の編集に応じて、メンバーへのポインターを形成するには、とを使用する必要が&ありclasskey::ます。通常の関数の場合、関数名からポインタから関数への暗黙の変換に相当するものはありません。

// not valid:
p.funcPointer3=TestFunc;

// valid:
p.funcPointer3 = &Child::TestFunc;

メンバーへのポインターを介してメンバーにアクセスするには、.*または->*演算子のいずれかを使用する必要があります。

例えば

(this->*p.funcPointer3)();
于 2010-11-28T11:54:44.060 に答える
1

Lambda関数funcPointer*の出現により、C++11でbind3つすべてを実際に実行できます。最初にそれぞれについて話し、彼らが何をしているのかを話し合いましょう。

  1. funcPointerオブジェクトを取り込まずChildメソッドを呼び出そうとするため、オブジェクトを保存する必要があります。子オブジェクトはポインタで保存できます:またはC ++ 14では値で保存できます:ChildChildbind(&Child::TestFunc, this)[arg = *this]() mutable { arg.TestFunc(); }
  2. funcPointer2Childを使用してメソッドを呼び出そうとしますParent*。これは次のように行うことができます。[](Parent* arg){ static_cast<Child*>(arg)->TestFunc(); }もちろん、これは合法ではないので、実際には、ラムダを呼び出す前に検証できるポリモーフィック型を作成する場合は、が実際にあると(new Parent)->TestFunc()想定しています。Parent*Child*Parent
[](Parent* arg) {
    assert(dynamic_cast<Child*>(arg) != nullptr);

    static_cast<Child*>(arg)->TestFunc();
}
  1. funcPointer3メソッドへのポインタを格納しようとしChildますが、すでにそれが機能しています。オブジェクトを使用しChildて呼び出す必要があります。例:(this->*p.funcPointer3)()funcPointer3ただし、次のように割り当てる必要がありますfuncPointer3 = &Child::TestFunc。これを実行しようとするとfuncPointer3 = &TestFunc、次のエラーが発生します。

'&':バインドされたメンバー関数式に対する不正な操作

次に、関数ポインターまたはメンバー関数ポインターを使用してクロージャー型を参照することはできないため、関数ポインターをfunctionオブジェクトに変換する必要があります。(これfuncPointer3 単なるメンバー関数ポインターなので、変換する必要はありませんが、functionオブジェクトにメンバー関数ポインターを含めることができp.funcPointer(this)、次の呼び出しを簡略化できることを示すために変換します。 ):

class Parent {
public:
    function<void()> funcPointer;
    function<void(Parent*)> funcPointer2;
    function<void(Child*)> funcPointer3;
};

適応したので、1、2、および3Parentで示すように簡単に割り当てることができます

void Child::Do() {
    Parent p;

    p.funcPointer = bind(&Child::TestFunc, this);
    p.funcPointer2 = [](Parent* arg) { static_cast<Child*>(arg)->TestFunc(); };
    p.funcPointer3 = &Child::TestFunc;
    p.funcPointer();
    p.funcPointer2(this);
    p.funcPointer3(this);
}

あなたはおそらくこれを知っていて、テストしているだけですが、で新しいオブジェクトを作成するのと同じくらい簡単に、Parentから継承したのメンバーを使用することができます。これを切り替えて、例のコードをスローします:http: //ideone.com/yD7RomChildParentChild::Do

于 2016-06-30T13:01:52.320 に答える