Reputation: 167
I have the following function call (myFunc() is of type double):
double value = F1->F2->myFunc();
I would like to get something that when i call value it should call F1->F2->myFunc();
With the help of the forum I got help and understood than calling a function through a variable i should do something like:
typedef double(*)() m;
m variable = myFunc;
and call it like variable();
Unfortunatly with the above setup it doesn't really work, I get a reference to &F2::myFunc but how do i get it in the above setup?
something like:
typedef double(*)() m;
m variable = &F2::myFunc; <- doesn't work :(
Thank you guys for any Help!
Upvotes: 0
Views: 113
Reputation: 126522
I would like to get something that when i call value it should call F1->F2->myFunc();
In C++11, you could use a lambda:
auto l = [&] () { return F1->F2->myFunc(); };
double value = l();
Scott Meyer's update on his upcoming book Effective C++ 11 also mentions:
Prefer lambdas to
std::bind
.
Upvotes: 3
Reputation: 409404
You could do this with std::function
and std::bind
:
std::function<double()> variable = std::bind(&ClassOfF2::myFunc, F1->F2);
double value = variable();
Upvotes: 4