Here is part of my code:
In a.h:
class classA
{
public:
void (*function_a)(void);
classA();
void function_a();
};
In a.cpp:
void classA::classA()
{
(*function_a)() = function_a;
}
void classA::function_a()
{
return;
}
I want to get function_a’s address and save it into void (*function_a)(void), but I got compile error that “expression is not assignable”. What shall I do to solve this problem?
First of all, choose different names for different things.
Second, the non-static member function pointer should be declared as:
then the assignment should be as:
and you call this as:
That is what you do in C++03.
However, in C++11, you can use
std::functionas well. Here is how you do it:and you call this as:
Online demo