简体   繁体   English

指向成员函数的指针

[英]Pointer to a member-function

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. 我想执行以下操作:我有两个类,A和B,并希望将A的功能绑定到B的功能,以便每当有人在B中调用该功能时,就会调用A的功能。

So basically, this is the scenario: ( important A and B should be independent classes) 因此,基本上,这是场景:( 重要的 A和B应该是独立的类)

This would be class A: 这将是A类:

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

This is class B 这是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? 我读过,这可以通过std :: function实现,这将如何工作? Also, does this have an impact in the performance whenever callTheFunction() is called? 另外,每当callTheFunction()时,这是否会对性能产生影响? In my example, its a audio-callback function which should call the sample-generating function of another class. 在我的示例中,它是一个音频回调函数,该函数应调用另一个类的样本生成函数。

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);

Solution based on usage C++11 std::function and std::bind. 基于用法的解决方案C ++ 11 std :: function和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 $ g ++ func.cpp -std = c ++ 11 -o func

Output: 输出:

$ ./func $ ./func

0 0 0 0

42 7 42 7

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 :)
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM