I was wondering if is possible to point a function of other structure into of a structure:
Example:
typedef struct
{
int func(int z)
{
r
The correct syntax for a pointer to method is:
&T::f
Where T
is a type declaring a method f
. Note that to be called, the pointer must be bound to an instance of T
, because the value of the pointer represents an offset to the beginning of an instance in memory.
In C++14, you may consider std::function
:
#include
struct sta
{
int func(int z)
{
return z * 2;
}
};
struct stah
{
std::function func;
};
int main()
{
sta sa;
stah sah;
sah.func = std::bind(&sta::func, &sa, std::placeholders::_1);
return 0;
}
You can also use lambdas instead of std::bind
:
int main()
{
sta sa;
stah sah;
sah.func = [&sa](int z) { return sa.func(z); };
return 0;
}
See std::function, std::bind, and std::placeholders on cppreference.com.