我想知道将算法封装到一个类中有多常见?更具体地说,不是有许多单独的函数在彼此之间转发公共参数:
void f(int common1, int param1, int *out1);
void g(int common1, int common2, int param1, int *out2)
{
f(common1, param1, ..);
}
将公共参数封装到一个类中,并在构造函数中完成所有工作:
struct Algo
{
int common1;
int common2;
Algo(int common1, int common2, int param)
{ // do most of the work }
void f(int param1, int *out1);
void g(int param1, int *out2);
};
不必通过函数参数转发公共参数和中间结果似乎非常实用。但我还没有看到这种“模式”被广泛使用。可能的缺点是什么?