Pointer to a member-function

旧城冷巷雨未停 提交于 2019-12-11 04:53:14

问题


I would like to do the following: I have two classes, A and B, and want to bind a function from A to a function from B so that whenever something calls the function in B, the function from A is called.

So basically, this is the scenario: (important A and B should be independent classes)

This would be class A:

class A {
private:
    // some needed variables for "doStuff"
public:
    void doStuff(int param1, float *param2);
}

This is class B

class B {
private:
    void callTheFunction();

public:
    void setTheFunction();   

}

And this is how I would like to work with these classes:

B *b = new B();
A *a = new A();

b->setTheFunction(a->doStuff); // obviously not working :(

I've read that this could be achieved with std::function, how would this work? Also, does this have an impact in the performance whenever callTheFunction() is called? In my example, its a audio-callback function which should call the sample-generating function of another class.


回答1:


Solution based on usage C++11 std::function and std::bind.

#include <functional>
#include <stdlib.h>
#include <iostream>

using functionType = std::function <void (int, float *)>;

class A
{
public:
    void doStuff (int param1, float * param2)
    {
        std::cout << param1 << " " << (param2 ? * param2 : 0.0f) << std::endl;
    };
};

class B
{
public:
    void callTheFunction ()
    {
        function (i, f);
    };

    void setTheFunction (const functionType specificFunction)
    {
        function = specificFunction;
    };

    functionType function {};
    int     i {0};
    float * f {nullptr};
};

int main (int argc, char * argv [])
{
    using std::placeholders::_1;
    using std::placeholders::_2;

    A a;
    B b;
    b.setTheFunction (std::bind (& A::doStuff, & a, _1, _2) );
    b.callTheFunction ();

    b.i = 42;
    b.f = new float {7.0f};
    b.callTheFunction ();

    delete b.f;
    return EXIT_SUCCESS;
}

Compile:

$ g++ func.cpp -std=c++11 -o func

Output:

$ ./func

0 0

42 7




回答2:


Here's a basic skeleton:

struct B
{
    A * a_instance;
    void (A::*a_method)(int, float *);

    B() : a_instance(nullptr), a_method(nullptr) {}

    void callTheFunction(int a, float * b)
    {
        if (a_instance && a_method)
        {
            (a_instance->*a_method)(a, b);
        }
    }
};

Usage:

A a;

B b;
b.a_instance = &a;
b.a_method = &A::doStuff;

b.callTheFunction(10, nullptr);



回答3:


This i basic a solution

class A {
private:
    // some needed variables for "doStuff"
public:
    void doStuff(int param1, float *param2)
    {

    }
};

typedef void (A::*TMethodPtr)(int param1, float *param2);

class B {
private:

    TMethodPtr m_pMethod;
    A* m_Obj;

    void callTheFunction()
    {
      float f;
      (m_Obj->*m_pMethod)(10, &f);
    }


public:
    void setTheFunction(A* Obj, TMethodPtr pMethod)
    {
       m_pMethod = pMethod;
       m_Obj = Obj;
    }
};

   void main()
   {
      B *b = new B();
      A *a = new A();
      b->setTheFunction(a, A::doStuff); // now work :)
   }


来源:https://stackoverflow.com/questions/24230726/pointer-to-a-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!