The question is the following: consider this piece of code:
#include <iostream>
class aClass
{
public:
void aTest(int a, int b)
{
printf("%d + %d = %d", a, b, a + b);
}
};
void function1(void (*function)(int, int))
{
function(1, 1);
}
void test(int a,int b)
{
printf("%d - %d = %d", a , b , a - b);
}
int main (int argc, const char* argv[])
{
aClass a();
function1(&test);
function1(&aClass::aTest); // <-- How should I point to a's aClass::test function?
return 0;
}
How can I use the a
's aClass::test
as an argument to function1
? I'm stuck in doing this.
I would like to access a member of the class.
There isn't anything wrong with using function pointers. However, pointers to non-static member functions are not like normal function pointers: member functions need to be called on an object which is passed as an implicit argument to the function. The signature of your member function above is, thus
rather than the type you try to use
One approach could consist in making the member function
static
in which case it doesn't require any object to be called on and you can use it with the typevoid (*)(int, int)
.If you need to access any non-static member of your class and you need to stick with function pointers, e.g., because the function is part of a C interface, your best option is to always pass a
void*
to your function taking function pointers and call your member through a forwarding function which obtains an object from thevoid*
and then calls the member function.In a proper C++ interface you might want to have a look at having your function take templated argument for function objects to use arbitrary class types. If using a templated interface is undesirable you should use something like
std::function<void(int, int)>
: you can create a suitably callable function object for these, e.g., usingstd::bind()
.The type-safe approaches using a template argument for the class type or a suitable
std::function<...>
are preferable than using avoid*
interface as they remove the potential for errors due to a cast to the wrong type.To clarify how to use a function pointer to call a member function, here is an example:
@Pete Becker's answer is fine but you can also do it without passing the
class
instance as an explicit parameter tofunction1
in C++ 11:I asked a similar question (C++ openframeworks passing void from other classes) but the answer I found was clearer so here the explanation for future records:
it’s easier to use std::function as in:
and then call as:
or even easier:
where you create a lambda that calls the object capturing it by reference
Since 2011, if you can change
function1
, do so, like this:(live demo)
Notice also that I fixed your broken object definition (
aClass a();
declares a function).A pointer to member function is different from a pointer to function. In order to use a member function through a pointer you need a pointer to it (obviously ) and an object to apply it to. So the appropriate version of
function1
would beand to call it:
I made the member function as static and all works: