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
  }
};

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

4

3 回答 3

8

You can't. You either pass a pointer to a static method or Parent has to accept also a pointer to the object.

You might want to look at boost::bind and boost::function for that:

#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();
于 2010-11-28T10:10:12.033 回答
3

为了响应您上次的编辑,要形成指向成员的指针,您必须使用&and classkey::。对于普通函数,没有与函数名称等效的指针到函数的隐式转换。

// not valid:
p.funcPointer3=TestFunc;

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

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

例如

(this->*p.funcPointer3)();
于 2010-11-28T11:54:44.060 回答
1

funcPointer*随着Lambda Functionsbind的出现,我们实际上可以在 C++11 中完成所有 3 个s 。让我们先谈谈每一个,并讨论他们在做什么:

  1. funcPointer试图在接收对象的情况下调用Child方法,因此必须保存对象。子对象可以通过指针保存:或者在 C++14 中可以通过值保存:ChildChildbind(&Child::TestFunc, this)[arg = *this]() mutable { arg.TestFunc(); }
  2. funcPointer2试图ChildParent*. 我们可以这样做:[](Parent* arg){ static_cast<Child*>(arg)->TestFunc(); }当然,这不会比(new Parent)->TestFunc()我们假设它Parent*实际上是 a更合法Child*,如果您愿意制作Parent一个多态类型,您可以在调用您的 lambda 之前进行验证:
[](Parent* arg) {
    assert(dynamic_cast<Child*>(arg) != nullptr);

    static_cast<Child*>(arg)->TestFunc();
}
  1. funcPointer3试图存储一个指向Child方法的指针,而你已经有了它的工作。你只需要使用一个Child对象来调用它,例如:(this->*p.funcPointer3)(). 但是您必须funcPointer3像这样分配:funcPointer3 = &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像在. 我将切换它并在示例中抛出代码:http: //ideone.com/yD7RomChildParentChild::Do

于 2016-06-30T13:01:52.320 回答