I have found myself to be in a situation where I need to pass a function to another function as an argument.
int callSomeFunction(int &func){
func();
}
If it makes any difference, callSomeFunction
is a class member.
class A{
A(){}
int callSomeFunction(int &func){
func();
}
~A(){}
};
A a();
a.callSomeFunction(func);
Ideally, callSomeFunction
would be able to take any kind of function.
template<typename T>
T callSomeFunction(T &func){
func();
}
I have tried many things to do this, Googled for several hours, all the standard stuff. I found these things but found them inconclusive as to the best way to accomplish this, or more appropriately the most efficient.
I like to use references over pointers where applicable, mostly because they are not a memory mess nor a syntactical mess in any cases. However, if pointers would be more applicable or a better solution, I welcome those answers as well. Thank you, any help or pointers on how to improve the question are also appreciated should you think it may help other people as well.