C++ 将指向类成员函数的指针作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18145874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:45:22  来源:igfitidea点击:

Passing a pointer to a class member function as a parameter

c++function-pointers

提问by ajay bidari

I have written a small program where I am trying to pass a pointer to member function of a class to another function. Can you please help me and where I am going wrong..?

我写了一个小程序,我试图将一个类的成员函数的指针传递给另一个函数。你能帮我吗,我哪里出错了..?

#include<iostream>
using namespace std;
class test{
public:
        typedef void (*callback_func_ptr)();
        callback_func_ptr cb_func;

        void get_pc();

        void set_cb_ptr(void * ptr);

        void call_cb_func();
};
void test::get_pc(){
         cout << "PC" << endl;
}
void test::set_cb_ptr( void *ptr){
        cb_func = (test::callback_func_ptr)ptr;
}
void test::call_cb_func(){
           cb_func();
}
int main(){
        test t1;
            t1.set_cb_ptr((void *)(&t1.get_pc));
        return 0;
}

I get the following error when I try to compile it.

当我尝试编译它时出现以下错误。

error C2276: '&' : illegal operation on bound member function expression

回答by Snps

You cannot cast a function pointer to void*.

您不能将函数指针强制转换为void*.

If you want a function pointer to point to a member functionyou must declare the type as

如果希望函数指针指向成员函数,则必须将类型声明为

ReturnType (ClassType::*)(ParameterTypes...)

Further you cannot declare a function pointer to a bound member function, e.g.

此外,您不能声明指向绑定成员函数的函数指针,例如

func_ptr p = &t1.get_pc // Error

Instead you must get the address like this:

相反,您必须获得如下地址:

func_ptr p = &test::get_pc // Ok, using class scope.

Finally, when you make a call to a function pointer pointing to a member function, you must call it with an instance of the class that the function is a member of. For instance:

最后,当您调用指向成员函数的函数指针时,您必须使用该函数所属的类的实例来调用它。例如:

(this->*cb_func)(); // Call function via pointer to current instance.


Here's the full example with all changes applied:

这是应用了所有更改的完整示例:

#include <iostream>

class test {
public:
    typedef void (test::*callback_func_ptr)();
    callback_func_ptr cb_func;
    void get_pc();
    void set_cb_ptr(callback_func_ptr ptr);
    void call_cb_func();
};

void test::get_pc() {
    std::cout << "PC" << std::endl;
}

void test::set_cb_ptr(callback_func_ptr ptr) {
    cb_func = ptr;
}

void test::call_cb_func() {
    (this->*cb_func)();
}

int main() {
    test t1;
    t1.set_cb_ptr(&test::get_pc);
    t1.call_cb_func();
}

回答by Attaque

In addition to Snps's answer, you could also use a function wrapperfrom C++11 to store a lambda function:

除了 Snps 的答案,您还可以使用C++11 中的函数包装器来存储 lambda 函数:

#include <iostream>
#include <functional>

class test
{
  public:
   std::function<void ()> Func;
   void get_pc();
   void call_cb_func();
   void set_func(std::function<void ()> func);
};

void test::get_pc()
{
  std::cout << "PC" << std::endl;
}

void test::call_cb_func()
{
  Func();
}

void test::set_func(std::function<void ()> func)
{
  Func = func;
}

int main() {
  test t1;
  t1.set_func([&](){ t1.get_pc(); });
  t1.call_cb_func();
}