避免"local variable as default parameter"的最佳设计?
Best design to avoid "local variable as default parameter"?
我正在编写一个近似函数,将两个不同的公差值作为参数:
bool Approximate(vector<PointC*>* pOutput, LineC input, double horizontalTolerance, double verticalTolerance)
如果未设置垂直容差,我希望函数设置垂直容差 = 水平容差。所以,我想完成这样的事情:
bool Approximate(vector<PointC*>* pOutput, LineC input, double horizontalTolerance, double verticalTolerance=horizontalTolerance)
我知道这是不可能的,因为局部变量不允许作为默认参数。所以我的问题是,设计这个功能的最佳方法是什么?
我想到的选项是:
不要使用默认参数,并让用户显式设置两个公差。
将"垂直容差"的默认值设置为负值,如果为负值,则重置为"水平容差":
bool Approximate(vector<PointC*>* pOutput, LineC input, double horizontalTolerance, double verticalTolerance=-1) { if (verticalTolerance < 0) { verticalTolerance = horizontalTolerance; } // Rest of function }
在我看来,第一点不是解决方案,而是旁路,第二点不可能是最简单的解决方案。
或者
你可以使用重载:
bool Approximate(vector<PointC*>* pOutput, LineC input,
double horizontalTolerance, double verticalTolerance)
{
//whatever
}
bool Approximate(vector<PointC*>* pOutput, LineC input,
double tolerance)
{
return Approximate(pOutput, input, tolerance, tolerance);
}
这完美地模仿了您想要实现的目标。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 在c代码之间共享数据的最佳方式
- 使用std::source_location报告错误的最佳实践
- 派生类销毁的最佳实践是什么
- 将寄存器设计成可由C和C++访问的外设的最佳实践
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 在C++中向零方向近似的最佳方法
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 从嵌套在std::映射中的std::列表中删除元素的最佳方式
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 检测win32服务创建和删除的最佳方法
- 在reactor中存储eventHandlers的最佳方式是什么
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 在AVX通道中混洗的最佳方式
- 避免"local variable as default parameter"的最佳设计?