Sometimes instead of virtualizing the differences, it is better to refactor the commonality (into helper functions):
Original | Virtual Refactor | Subroutines |
---|---|---|
int D1::f()
{
common...;
specialized...;
same...;
}
int D2::f()
{
common...;
different...;
same...;
}
|
int Base::f()
{
common...;
vf();
same...;
}
int D1::vf() override
{
specialized...;
}
int D2::vf() override
{
different...;
}
|
void common()
{
common...
}
void same()
{
same...
}
int D1::f()
{
common();
specialized...;
same();
}
int D2::f()
{
common();
different...;
same();
}
|
Sometimes.