How Can I Pass a Member Function to a Function Pointer?

后端 未结 3 2038
温柔的废话
温柔的废话 2020-12-01 16:57
class Child;
class Parent
{
public:
  void (*funcPointer)();
  void (*funcPointer2)(Parent* _this);
  void (Child::*funcPointer3)();
};

class Child: public Parent
{         


        
3条回答
  •  天涯浪人
    2020-12-01 17:34

    We can actually accomplish all 3 of your funcPointer*s in C++11 with the advent of bind and Lambda Functions. Let's talk about each one first and discuss what they are doing:

    1. funcPointer seeks to call a Child method without taking in a Child object, so the Child object would have to be saved. The child object could be saved by pointer: bind(&Child::TestFunc, this) Or in C++14 it could be saved by value: [arg = *this]() mutable { arg.TestFunc(); }
    2. funcPointer2 seeks to call a Child method with a Parent*. We could do this like: [](Parent* arg){ static_cast(arg)->TestFunc(); } Of course this wouldn't be any more legal than (new Parent)->TestFunc() so we're assuming that the Parent* is in reality a Child*, if you were willing to make Parent a Polymorphic Type you could verify before calling in your lambda:
    [](Parent* arg) {
        assert(dynamic_cast(arg) != nullptr);
    
        static_cast(arg)->TestFunc();
    }
    
    1. funcPointer3 seeks to store a pointer to a Child method, and you already had that working. You just needed to use a Child object to call it, for example: (this->*p.funcPointer3)(). But you must assign funcPointer3 like this: funcPointer3 = &Child::TestFunc, cause if you try to do this: funcPointer3 = &TestFunc you will get the error:

    '&': illegal operation on bound member function expression

    Next, a function pointer or a member function pointer cannot be used to reference a Closure Type, so we'll need to convert your function pointers to function objects. (funcPointer3 is just a member function pointer, so it doesn't need to be converted, but I will convert it to demonstrate that a function object can contain a member function pointer and it simplifies the call to: p.funcPointer(this)):

    class Parent {
    public:
        function funcPointer;
        function funcPointer2;
        function funcPointer3;
    };
    

    Now that we've adapted Parent we can easily assign as demonstrated in 1, 2, and 3:

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

    You probably know this and were just testing, but we could have just as easily used the members of the Parent that Child inherited from as we could create a new Parent object in Child::Do. I'm going to switch that up and throw the code in an example: http://ideone.com/yD7Rom

提交回复
热议问题