У меня есть код, как:C++ лучший способ справиться с функцией перегрузки
void Foo(int& a, string& b, vector<int>& c) {
... // 30 lines of code are same as another function
... // 10 lines of code to parse and assign value to vector<int>& c
}
void Foo(int& a, string& b, map<string, int>& d) {
... // 30 lines of code are same as another function
... // 10 lines of code to parse and assign value to map<string, int>& d
}
Есть ли способ, чтобы избежать повторения, что 30 строк кода? Должен ли я использовать функцию перегрузки в этом случае?
EDIT:
Что делать, если код не так легко отделить? Нравится:
void Foo(int& a, string& b, vector<int>& c) {
for() {
if(m) ... // 30 lines of code are same as another function
else if(n) ... // 30 lines of code are same as another function
else if(o) ... // 30 lines of code are same as another function
else if(p) ... // 10 lines of 'vector<int>& c' code
else if(q) ... // 10 lines of 'vector<int>& c' code
}
}
void Foo(int& a, string& b, map<string, int>& d) {
for() {
if(m) ... // 30 lines of code are same as another function
else if(n) ... // 30 lines of code are same as another function
else if(o) ... // 30 lines of code are same as another function
else if(p) ... // 10 lines of 'map<string, int>& d' code
else if(q) ... // 10 lines of 'map<string, int>& d' code
}
}
Почему вы не ставите общие 30 строк в отдельную функцию? – Asha
Поместите 30 строк общего кода в отдельную функцию, которая вызывается функциями 'Foo'? –