說,我有兩個功能:通過元件的位置的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' –