1

C++ の質問: 次のプロトタイプを持つ関数があるとします: void f(int=10, int=20, int=30,int=40) この関数が 2 つの引数を渡すことによって呼び出される場合、これらの引数をどのように確認できますか?は 1 番目と 3 番目として扱われますが、2 番目の広告と 4 番目の広告はデフォルトとして使用されます。

関数のオーバーロードを使用してこの問題を解決する考えがありますが、この問題を解決する直接的な方法があるかどうか知りたいですか?? c++ に関する回答を探しています

4

1 に答える 1

2

The solution with overloading would look like this:

void f(int a, int b, int c, int d=40)
{
  // ...
}

void f(int a=10, int c=30)
{
  f(a,20,c);
}

I don't think any other solution/work-around would be easier. There are libraries to provide named parameters, but they change the way you call the function and impose quite some overhead at least to the compiler if not to the generated code itself.

于 2013-10-05T08:13:28.583 に答える