Suppose I have a class:
class A {
public:
A();
void myFunc();
};
Then at a different point in the program I create an instance of class A, and attempt to obtain a function pointer to myFunc()
:
A* a = new A;
//ATTEMPTS AT RETRIVING FUNCTION POINTER FOR A::MYFUNC
std::function<void()> fnptr = a->myFunc;
std::function<void()> fnptr(std::bind(A::myFunc, &a);
But both of these attempts present the error "call to non static member function without object argument"
Perhaps both of these attempts are totally off the mark, but essentially im trying to obtain a function pointer to the myFunc()
function in the specific instance of a
. Any help on how to do so is appreciated :)
You need the following syntax:
std::function<void()> fnptr(std::bind(&A::myFunc, a));
You could also use a lambda expression like this:
auto fn = [a] { a->myFunc(); };
Here's a demo.