Say, I have two functions:
int methodA(int);
int methodB(int);
To avoid repetition of the given below big chunk of code I want create a separate function (say, funcToAvoidRepeatedCode
), which takes function pointer:
{
//...many lines of code
std::multimap< size_t, std::pair<char, size_t> >::iterator it;
//...many lines of code
methodA(it->first); OR methodB(it->second.second); // << This is the only difference.
//...many lines of code
}
I know how to pass the function pointer using std::function
. I am looking to change the above lines of code into this form:
void funcToAvoidRepeatedCode(funcPtr, ?????){
//...many lines of code
std::multimap< size_t, std::pair<timelineWeakRef, size_t> >::iterator it;
//...many lines of code
funcPtr(???????);
^~~~~~What kind of parameter I can pass to
funcToAvoidRepeatedCode() to differentiate the
position (first or second.second) in map element?
//...many lines of code
}
How do I accomplish this?
I may be missing something, but you clearly have some kind of condition there that indicates whether you should use
methodA
ormethodB
. So why don't you pass that condition into the function instead and avoid using function pointers altogether.Passing a function pointer would be required if an arbitrary function of some signature can be passed (e.g. the comparator in
sort()
), but in this case it is not needed.