说,我有两个功能:通过元件的位置的std ::地图作为参数来调用函数
int methodA(int);
int methodB(int);
为了避免下面的代码大块我想创建一个单独的函数在给定的重复(比如,funcToAvoidRepeatedCode
),这需要函数指针:
{
//...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
}
我知道如何通过使用std::function
函数指针。我期待将以上代码行更改为以下格式:
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
}
我该如何做到这一点?
请澄清应该触发'methodA'的条件是什么,以及何时应该调用'methodB' –
这个大代码是两个'switch-case'的一部分。所以,我试图让它们简洁。请注意,还有其他'switch-case's以及不使用此代码。所以我不能在'switch'之前或之后放置它,因为它在所有情况下都不常见。 –
是的,但它不是很清楚**当你希望'methodA'被调用和**时**'methodB' –