How Can I Pass a Member Function to a Function Pointer?
Asked Answered
H

3

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

How can I pass a member function to a function pointer, and then how would I then call that function pointer?

Heilner answered 28/11, 2010 at 10:7 Comment(1)
@T.J.Crowder You can find the corrected C++ FAQ link here: isocpp.org/wiki/faq/pointers-to-members#memfnptr-vs-fnptr But again I believe it is irrelevant as it is warning against the passing of member function pointers, not the using of member function pointers.Conifer
U
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();
Uproot answered 28/11, 2010 at 10:10 Comment(6)
also added a non-boost version.Uproot
it's really what i want! i'm coding in visual studio c++ 6 now, i'm not sure it support the boost::bind and boost:function. do you know?Heilner
I added the required includes, so you simply need to get boost and try it out. But it looks like boostpro.com/download does not support VS6.0, but that doesn't mean bind and function can't work there.Uproot
Might I add that MSVS 2010 or GCC 4.4+ adds support for std::function and std::bind from C++0X, which are a future-proof standard way of doing this kind of thing.Mud
good point. But that feature definitely will never be available in MSVC6 :-)Uproot
@Mud And might I add the all important lambda in C++11. And with C++14 lambdas, you could even capture this by value. I've written an answer which will be of absolutely no help on Visual C++ 6.0, but is the de facto answer for modern compilers.Conifer
A
3

In response to your last edit, to form a pointer-to-member, you have to use & and classkey::. There's no equivalent to the function name to pointer-to-function implicit conversion for normal functions.

// not valid:
p.funcPointer3=TestFunc;

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

To access a member through a pointer-to-member you have to use either the .* or ->* operator.

E.g.

(this->*p.funcPointer3)();
Artina answered 28/11, 2010 at 11:54 Comment(2)
this really works, I have to tell the compiler which object the member function pointer to work with. thank you!Heilner
@Heilner This was the best answer before C++11 there is really no need to bloat your executables with Boost.Conifer
C
1

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<Child*>(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<Child*>(arg) != nullptr);

    static_cast<Child*>(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<void()> funcPointer;
    function<void(Parent*)> funcPointer2;
    function<void(Child*)> 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<Child*>(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

Conifer answered 30/6, 2016 at 13:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.