And it worked fine. Then I tried with class function and there was an problem. So I searched the internet for a solution and I found I need to make static functin. It worked. My question is how can I store different class function pointers without making function static?
Example by JBorges worked for me. Make sure that you have C++11 enabled. If you use gcc or clang++, add -std=c++11 option to command line.
Without C++11 you can use function pointers to call class member function:
for class
1 2 3 4
class A {
public:
int member_function(int arg);
};
pointer to A::member_function can be declared and called:
1 2 3 4 5 6
int main()
{
int (A::*pointer)(int arg) = &A::member_function;
A a;
(a.*pointer)(1);
}
alternatively with typedef:
1 2 3 4 5 6 7
typedefint (A::*fpointer)(int arg);
int main()
{
A a;
fpointer pointer = &A::member_func;
(a.*pointer)(1);
}