【发布时间】:2014-09-17 02:22:57
【问题描述】:
我有一个看起来像这样的函数:
bool generate_script (bool net, bool tv, bool phone,
std::string clientsID,
std::string password,
int index, std::string number,
std::string Iport, std::string sernoID,
std::string VoiP_number, std::string VoiP_pass,
std::string target, int slot, int port,
int onu, int extra, std::string IP, std::string MAC);
在我看来,它看起来很丑。处理这个问题的正确方法是什么?我应该创建几个具有不同数据类型(int、string 和 bool)的向量并将它们作为参数传递给这个函数吗?
【问题讨论】:
-
我不鼓励使用向量,结构会是更合适的解决方案。
-
还有 boost 命名参数,如果你有很多有用的默认值
-
将它们分组为一个或多个
structs。另外,不要忘记对字符串使用const引用类型,以防您不在函数中修改它,这样可以提高速度。 -
如果这个函数的唯一目的是处理一个结构,为什么不把它变成那个结构的方法呢?
-
似乎没有人提到的是它们都是按值传递的 :( 通过引用传递可以避免这么多的分配
标签: c++ function argument-passing