C++ 函数参数默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2842928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:14:43  来源:igfitidea点击:

Default value of function parameter

c++syntaxoptional-parameters

提问by httpinterpret

1.

1.

int Add (int a, int b = 3);
int Add (int a, int b)
{

}

2.

2.

int Add (int a, int b);
int Add (int a, int b = 3)
{

}

Both work; which is the standard way and why?

两者都有效;这是标准方式,为什么

回答by Greg Hewgill

If you put the declaration in a header file, and the definition in a separate .cppfile, and #includethe header from a different .cppfile, you will be able to see the difference.

如果您将声明放在一个头文件中,将定义放在一个单独的.cpp文件中,并将#include头文件放在不同的.cpp文件中,您将能够看到差异。

Specifically, suppose:

具体来说,假设:

lib.h

库文件

int Add(int a, int b);

lib.cpp

库文件

int Add(int a, int b = 3) {
   ...
}

test.cpp

测试.cpp

#include "lib.h"

int main() {
    Add(4);
}

The compilation of test.cppwill not see the default parameter declaration, and will fail with an error.

的编译test.cpp不会看到默认参数声明,并且会失败并报错。

For this reason, the default parameter definition is usually specified in the function declaration:

为此,通常在函数声明中指定默认参数定义:

lib.h

库文件

int Add(int a, int b = 3);

回答by AnT

In C++ the requirements imposed on default arguments with regard to their location in parameter list are as follows:

在 C++ 中,关于默认参数在参数列表中的位置的要求如下:

  1. Default argument for a given parameter has to be specified no more than once. Specifying it more than once (even with the same default value) is illegal.

  2. Parameters with default arguments have to form a contiguous group at the end of the parameter list.

  1. 给定参数的默认参数必须指定不超过一次。多次指定(即使使用相同的默认值)也是非法的。

  2. 带有默认参数的参数必须在参数列表的末尾形成一个连续的组。

Now, keeping that in mind, in C++ you are allowed to "grow" the set of parameters that have default arguments from one declaration of the function to the next, as long as the above requirements are continuously satisfied.

现在,请记住这一点,在 C++ 中,只要持续满足上述要求,就可以将具有默认参数的参数集从函数的一个声明“增长”到下一个。

For example, you can declare a function with no default arguments

例如,您可以声明一个没有默认参数的函数

void foo(int a, int b);

In order to call that function after such declaration you'll have to specify both arguments explicitly.

为了在这样的声明之后调用该函数,您必须明确指定两个参数。

Later (further down) in the same translation unit, you can re-declare it again, but this time with one default argument

稍后(进一步向下)在同一翻译单元中,您可以再次重新声明它,但这次使用一个默认参数

void foo(int a, int b = 5);

and from this point on you can call it with just one explicit argument.

从现在开始,您可以只用一个显式参数来调用它。

Further down you can re-declare it yet again adding one more default argument

再往下,您可以重新声明它,再添加一个默认参数

void foo(int a = 1, int b);

and from this point on you can call it with no explicit arguments.

从现在开始,您可以在没有明确参数的情况下调用它。

The full example might look as follows

完整示例可能如下所示

void foo(int a, int b);

int main()
{
  foo(2, 3);

  void foo(int a, int b = 5); // redeclare
  foo(8); // OK, calls `foo(8, 5)`

  void foo(int a = 1, int b); // redeclare again
  foo(); // OK, calls `foo(1, 5)`
}

void foo(int a, int b)
{
  // ...
}

As for the code in your question, both variants are perfectly valid, but they mean different things. The first variant declares a default argument for the second parameter right away. The second variant initially declares your function with no default arguments and then adds one for the second parameter.

至于您问题中的代码,两种变体都是完全有效的,但它们的含义不同。第一个变体立即为第二个参数声明了一个默认参数。第二个变体最初声明您的函数没有默认参数,然后为第二个参数添加一个。

The net effect of both of your declarations (i.e. the way it is seen by the code that follows the second declaration) is exactly the same: the function has default argument for its second parameter. However, if you manage to squeeze some code between the first and the second declarations, these two variants will behave differently. In the second variant the function has no default arguments between the declarations, so you'll have to specify both arguments explicitly.

您的两个声明的最终效果(即第二个声明之后的代码所看到的方式)完全相同:该函数的第二个参数具有默认参数。但是,如果您设法在第一个和第二个声明之间压缩一些代码,这两个变体的行为将有所不同。在第二个变体中,函数在声明之间没有默认参数,因此您必须明确指定两个参数。

回答by user342264

The first way would be preferred to the second.

第一种方式比第二种方式更受欢迎。

This is because the header file will show that the parameter is optional and what its default value will be. Additionally, this will ensure that the default value will be the same, no matter the implementation of the corresponding .cpp file.

这是因为头文件将显示该参数是可选的以及它的默认值是什么。此外,这将确保默认值相同,无论相应的 .cpp 文件的实现如何。

In the second way, there is no guarantee of a default value for the second parameter. The default value could change, depending on how the corresponding .cpp file is implemented.

在第二种方式中,不能保证第二个参数的默认值。默认值可能会更改,具体取决于相应的 .cpp 文件的实现方式。

回答by clyfe

Default arguments must be specified with the first occurrence of the function name—typically, in the function prototype. If the function prototype is omitted because the function definition also serves as the prototype, then the default arguments should be specified in the function header.

默认参数必须在函数名第一次出现时指定——通常在函数原型中。如果因为函数定义也充当原型而省略函数原型,则应在函数头中指定默认参数。