use*_*536 2 c++ refactoring common-code
我有两种方法f(vector<int>& x, ....) and g(DBConn& x, ....)
,(....)参数都是相同的.
两个方法中的代码完全相同,除了一个语句,我们根据x的类型执行不同的操作:
in f(): we do x.push_back(i)
in g(): we do x.DeleteRow(i)
Run Code Online (Sandbox Code Playgroud)
将公共代码提取到一个方法中但有两个不同语句的最简单方法是什么?
我想有一个模板运算符重载operator()(int a),但这似乎有点过分.
common_function(....)
{
}
f(vector<int>x,... )
{
x.push_back(i);
common_f(...);
}
g(DBConn& x, ....)
{
x.DeleteRow(i);
common_f(...);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
225 次 |
| 最近记录: |