如何将成员函数传递给函数指针?

10
class Child;
class Parent
{
public:
  void (*funcPointer)();
  void (*funcPointer2)(Parent* _this);
  void (Child::*funcPointer3)();
};

class Child: public Parent
{
public:
  void TestFunc(){

  }
  void Do(){
    Parent p;
    p.funcPointer=TestFunc; // error, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(void)'
    p.funcPointer2=TestFunc; // error too, '=': cannot convert from 'void (__thiscall Child::* )(void)' to 'void (__cdecl *)(Parent *)'
    p.funcPointer3=TestFunc; //this works
    p.funcPointer3=&Child::TestFunc; // this works too.
    p.funcPointer3();    // error, term does not evaluate to a function taking 0 arguments
  }
};

我该如何将成员函数传递给函数指针,然后如何调用该函数指针?


@T.J.Crowder,您可以在此处找到已更正的C++ FAQ链接:https://isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr 但我认为这并不相关,因为它是针对成员函数指针的传递而发出的警告,而不是使用成员函数指针。 - Jonathan Mee
3个回答

8
你不能这样做。你要么传递一个静态方法的指针,要么父类也必须接受对象的指针。
你可能需要查看 boost::bindboost::function
#include <boost/bind.hpp>
#include <boost/function.hpp>
struct Y
{
    void say(void) { std::cout << "hallo!";}

    boost::function<void()> getFunc() { return boost::bind(&Y::say, this); }
};

struct X
{
    //non-boost:
    void (Y::*func)();
    Y* objectY;
    void callFunc() { (objectY->*func)(); }

    //boost version:
    boost::function<void()> f;

};


X x;
Y y;
x.f = boost::bind(&Y::say, boost::ref(y));
x.f = y.getFunc();
x.f();
x.func = &Y::say;
x.objectY = &y; 
x.callFunc();

2
也添加了一个非boost版本。 - Philipp
这真的是我想要的!我现在正在使用Visual Studio C++ 6编码,不确定它是否支持boost::bind和boost:function。你知道吗? - lovespring
我已添加所需的包含文件,因此您只需要获取boost并尝试使用它。但是看起来http://www.boostpro.com/download/不支持VS6.0,但这并不意味着bind和function不能在那里工作。 - Philipp
1
我想补充一点,MSVS 2010或GCC 4.4+支持C++0X中的std::functionstd::bind,这是一种未来可靠的标准方法来完成这种操作。 - rubenvb
好的观点。但是这个功能肯定不会在MSVC6中提供 :-) - Philipp
@rubenvb,我还要补充一点,在C++11中非常重要的lambda表达式。而且在C++14中,你甚至可以通过值捕获this。我已经写了一个答案,对于Visual C++ 6.0没有任何帮助,但对于现代编译器来说是事实上的答案。 - Jonathan Mee

3
作为对您最后一次编辑的回应,要形成一个指向成员的指针,您必须使用 &classkey::。对于普通函数,没有与函数名到指向函数的指针的隐式转换相当的等价物。
// not valid:
p.funcPointer3=TestFunc;

// valid:
p.funcPointer3 = &Child::TestFunc;

通过指向成员的指针访问成员,您需要使用 .*->* 运算符。

例如:

(this->*p.funcPointer3)();

这真的很有效,我必须告诉编译器要使用哪个对象的成员函数指针。谢谢! - lovespring
@lovespring 在C++11之前,这是最好的答案,真的没有必要用Boost使你的可执行文件变得臃肿。 - Jonathan Mee

1
我们实际上可以在C++11中通过bindLambda Functions来完成你的三个funcPointer*。首先让我们分别讨论每一个并讨论它们的作用:
  1. funcPointer旨在调用一个Child方法,不需要传入一个Child对象,因此需要保存Child对象。可以通过指针来保存子对象:bind(&Child::TestFunc, this)。或者在C++14中可以按值保存:[arg = *this]() mutable { arg.TestFunc(); }
  2. funcPointer2旨在使用Parent*调用Child方法。我们可以这样做:[](Parent* arg){ static_cast<Child*>(arg)->TestFunc(); }当然,这并不比(new Parent)->TestFunc()更合法,所以我们假设Parent*实际上是一个Child*,如果您愿意将Parent设置为多态类型,则可以在调用lambda之前进行验证:
[](Parent* arg) {
    assert(dynamic_cast<Child*>(arg) != nullptr);

    static_cast<Child*>(arg)->TestFunc();
}
  1. funcPointer3旨在存储指向Child方法的指针,而您已经做到了这一点。 您只需要使用一个Child对象来调用它,例如:(this->*p.funcPointer3)()。 但是您必须像这样分配funcPointer3funcPointer3 = &Child :: TestFunc,因为如果您尝试这样做:funcPointer3 = &TestFunc,则会出现错误:

'&':对绑定成员函数表达式的非法操作

接下来,函数指针或成员函数指针不能用于引用闭包类型,因此我们需要将您的函数指针转换为function对象。(funcPointer3 只是一个成员函数指针,所以不需要转换,但我会将它转换来演示一个function对象可以包含一个成员函数指针,并且简化了调用:p.funcPointer(this)):

class Parent {
public:
    function<void()> funcPointer;
    function<void(Parent*)> funcPointer2;
    function<void(Child*)> funcPointer3;
};

现在我们已经适应了Parent,可以轻松地像123演示的那样进行分配。
void Child::Do() {
    Parent p;

    p.funcPointer = bind(&Child::TestFunc, this);
    p.funcPointer2 = [](Parent* arg) { static_cast<Child*>(arg)->TestFunc(); };
    p.funcPointer3 = &Child::TestFunc;
    p.funcPointer();
    p.funcPointer2(this);
    p.funcPointer3(this);
}

你可能已经知道这一点并且只是在测试,但我们可以像创建一个新的Parent对象那样使用Child从中继承的Parent成员,在Child::Do中。我将更改它并将代码放在示例中:http://ideone.com/yD7Rom

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接