c ++传递成员函数指针作为参数

时间:2014-11-23 03:16:15

标签: function pointers parameters

我已经阅读了不少帖子成员函数指针,但似乎没有一个提议的解决方案适用于我的情况。我学会了如何在这些帖子中声明成员函数指针,所以我有一个像这样的类A:

Class A {
    // some members over here
    B  registar;

    int (A::cb) (int, void*);      // my function pointer

    int callback(int, void* args); // the function I need to call in method1()

    A(): cb(&A::callback) {}

    int method1()
    { 
        //need to access member variables here and construct args
        char* args;

        // this is where I need to pass callback(int, void*) as a parameter to somewhere.
        // what I want to achieve is something like 
        // registar.register(callback, args)
    } 
}

所以我试图将回调作为参数传递给method1()中的某个函数,如上所示。 我该怎么做呢?

我看的例子是

class A {
    public:
    int f();
     int (A::*x)(); // <- declare by saying what class it is a pointer to
};

int A::f() { return 1;}

int main() {
     A a;
     a.x = &A::f; // use the :: syntax
     printf("%d\n",(a.*a.x)()); // use together with an object of its class
} 

我已尝试过上述语法(this-&gt;(* this.cb))。它没有用。

0 个答案:

没有答案