2

2 つの異なる許容値をパラメーターとして取る近似関数を作成しています。

bool Approximate(vector<PointC*>* pOutput, LineC input, double horizontalTolerance, double verticalTolerance)

verticalTolerance が設定されていない場合、関数で verticalTolerance = horizo​​ntalTolerance を設定する必要があります。だから、私は次のようなことを達成したい:

bool Approximate(vector<PointC*>* pOutput, LineC input, double horizontalTolerance, double verticalTolerance=horizontalTolerance)

ローカル変数はデフォルトのパラメータとして許可されていないため、これが不可能であることはわかっています。私の質問は、この関数を設計する最良の方法は何ですか?

私が考えたオプションは次のとおりです。

  1. デフォルトのパラメーターを使用せず、ユーザーが両方の許容値を明示的に設定するようにします。

  2. verticalTolerance のデフォルト値を負の値に設定し、負の場合は horizo​​ntalTolerance にリセットします。

    bool Approximate(vector<PointC*>* pOutput, LineC input, double horizontalTolerance, double verticalTolerance=-1)
    {
        if (verticalTolerance < 0)
        {
            verticalTolerance = horizontalTolerance;
        }
        // Rest of function
    }
    

私の意見では、ポイント 1 は解決策ではなくバイパスであり、ポイント 2 は最も単純な解決策ではありません。

4

1 に答える 1

7

または、オーバーロードを使用できます。

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);
}

これは、達成したいことを完全に模倣します。

于 2012-06-12T09:21:13.343 に答える