C ++传递成员函数作为参数 [英] C++ passing member function as argument

查看:296
本文介绍了C ++传递成员函数作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

C ++,成员函数的函数指针

问题如下:考虑这段代码:

The question is the following: consider this piece of code:

#include <iostream>


class aClass
{
public:
    void aTest(int a, int b)
    {
        printf("%d+%d=%d",a,b,a+b);
    }
};

void function1(void (*function)(int,int))
{
    function(1,1);
}

void test(int a,int b)
{
    printf("%d-%d=%d",a,b,a-b);
}

int main (int argc, const char * argv[])
{
    aClass a();

    function1(&test);
    function1(&aClass::aTest ); // <-- how should I point to a's aClass::test function?

    return 0;
}

如何使用a的aClass :: test作为function1的参数?

How can I use the a's aClass::test as an argument to function1? I'm stuck in doing this.

推荐答案

我想要访问该类的成员。解决方案

解决方案

使用函数指针没有什么问题。然而,指向非静态成员函数的指针不像正常的函数指针:成员函数需要在作为函数的隐式参数传递的对象上调用。上面你的成员函数的签名是,因此

There isn't anything wrong with using function pointers. However, pointers to non-static member functions are not like normal function pointers: member functions need to be called on an object which is passed as an implicit argument to the function. The signature of your member function above is, thus

void (aClass::*)(int, int)

而不是您尝试使用的类型

rather than the type you try to use

void (*)(int, int)

使成员函数 static 在这种情况下它不需要任何对象被调用,你可以使用类型 void(*) (int,int)

One approach could consist in making the member function static in which case it doesn't require any object to be called on and you can use it with the type void (*)(int, int).

如果您需要访问课程的任何非静态成员需要坚持使用函数指针,例如,因为函数是C接口的一部分,你最好的选择是总是传递一个 void * 给你的函数接受函数指针和调用你的成员通过一个转发函数从 void * 获得一个对象,然后调用成员函数。

If you need to access any non-static member of your class and you need to stick with function pointers, e.g., because the function is part of a C interface, your best option is to always pass a void* to your function taking function pointers and call your member through a forwarding function which obtains an object from the void* and then calls the member function.

一个合适的C ++接口,你可能想看看使你的函数采取模板参数为函数对象使用任意类类型。如果使用模板化接口是不希望的,你应该使用 std :: function< void(int,int)> :你可以为这些创建一个合适的可调用的函数对象,例如,使用 std :: bind()

In a proper C++ interface you might want to have a look at having your function take templated argument for function objects to use arbitrary class types. If using a templated interface is undesirable you should use something like std::function<void(int, int)>: you can create a suitably callable function object for these, e.g., using std::bind().

使用模板参数类型或合适的 std :: function< ...> 比使用 void *

The type-safe approaches using a template argument for the class type or a suitable std::function<...> are preferable than using a void* interface as they remove the potential for errors due to a cast to the wrong type.

为了说明如何使用函数指针来调用成员函数,下面是一个例子:

To clarify how to use a function pointer to call a member function, here is an example:

// the function using the function pointers:
void somefunction(void (*fptr)(void*, int, int), void* context) {
    fptr(context, 17, 42);
}

void non_member(void*, int i0, int i1) {
    std::cout << "I don't need any context! i0=" << i0 << " i1=" << i1 << "\n";
}

struct foo {
    void member(int i0, int i1) {
        std::cout << "member function: this=" << this << " i0=" << i0 << " i1=" << i1 << "\n";
    }
};

void forwarder(void* context, int i0, int i1) {
    static_cast<foo*>(context)->member(i0, i1);
}

int main() {
    somefunction(&non_member, 0);
    foo object;
    somefunction(&forwarder, &object);
}

这篇关于C ++传递成员函数作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆