将任何泛型函数作为C ++参数传递

时间:2014-09-03 11:01:42

标签: c++ function pointers arguments member

我试图将任何泛型函数作为C ++函数中的参数传递。作为一个额外的乐趣层,该函数将驻留在它自己独立的类中。在这种特殊情况下,他们都不会参数并返回void。这包括任何泛型类的成员。

我目前的代码如下:

class functionsFedHere {
public:
    void (*callback)();
    functionsFedHere(void(*)());
}

functionsFedHere::functionsFedHere (void(*callbackFunction)()) {
    callback = callbackFunction;
}

void fn1() { cout<<"Fn1"<<endl; }

class myClass {
public:
    int i;
    void fn2() { cout<<i<<endl; }
}

class myOtherClass {
public:
    string j;
    void fn3() { cout<<j<<endl; }
}

int main() {

    // Initialise some objects
    myClass b;
    b.i = 2;

    myOtherClass c;
    c.j = "some text";

    // Works fine with non-member functions
    functionsFedHere objectA(fn1);
    objectA.callback();

    // Doesn't work with member functions
    functionsFedHere objectB(b.fn2);
    functionsFedHere objectC(c.fn3);
}

我见过像a forwarding functionboost::bind这样的解决方案,但据我所知,我认为这些解决方案不合适?

值得注意的是,最终我想通过指针数组传递成员函数。例如,如果myPointer[]是指向类myClass的对象的指针数组,那么能够编写类似的内容会很好:

functionsFedHere objectD(myPointer[0]->fn2);

编辑:显然我还不够清楚。 This answer不是一个合适的答案,因为我希望将成员函数和非成员函数作为参数传递(而建议的答案是设置指向同一函数的函数的成员指针)类)。

我不认为前向函数示例会起作用,因为转发函数假设一个类类型,我希望传递泛型类的对象。

boost::bind很可能就是答案;我对此并不熟悉。有人能指出我对一些新手友好的阅读材料吗?

编辑2 :很抱歉,忘了提及我在 pre-C ++ 11 的设备上进行编程。

1 个答案:

答案 0 :(得分:4)

使用std::function

class functionsFedHere {
public:
    typedef std::function<void()> Func;
    Func callback;
    functionsFedHere(Func callback_) : callback(callback_) {}
};

void fn1() { cout<<"Fn1"<<endl; }

class myClass {
public:
    int i;
    void fn2() { cout<<i<<endl; }
};

class myOtherClass {
public:
    string j;
    void fn3() { cout<<j<<endl; }
};

class callableClass {
public:
    void operator()() { std::cout << "in callableClass" << std::endl; }
};

int main() {

    // Initialise some objects
    myClass b;
    b.i = 2;

    myOtherClass c;
    c.j = "some text";

    // Works fine with non-member functions
    functionsFedHere objectA(fn1);
    objectA.callback();

    // Works with member functions now
    functionsFedHere objectB(std::bind(&myClass::fn2, b));
    objectB.callback();
    functionsFedHere objectC(std::bind(&myOtherClass::fn3, c));
    objectC.callback();

    // Works with lambdas as well
    functionsFedHere objectLambda([]() { std::cout << "in lambda" << std::endl; });
    objectLambda.callback();

    // Works also with classes with overloaded operator()
    functionsFedHere(callableClass()).callback();
    return 0;
}