Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ refactor common code with one different statement

I have two methods f(vector<int>& x, ....) and g(DBConn& x, ....) where the (....) parameters are all identical.

The code inside the two methods are completely identical except for one statement where we do different actions based on the type of x:

in f(): we do x.push_back(i)
in g(): we do x.DeleteRow(i)

What is the simplest way to extract the common code into one method and yet have the two different statements?

I am thinking of having a templated functor that overloads operator () (int a) but that seems overkill.

like image 539
user231536 Avatar asked Jan 22 '26 04:01

user231536


1 Answers

common_function(....)
{
}

f(vector<int>x,... )
{
    x.push_back(i);
    common_f(...);
}
g(DBConn& x, ....)
{
    x.DeleteRow(i);
    common_f(...);
}
like image 82
Arseny Avatar answered Jan 24 '26 20:01

Arseny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!