函数参数默认值

发布于 2024-09-01 16:17:51 字数 262 浏览 3 评论 0原文

1.

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

}

2.

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

}

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

1.

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

}

2.

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

}

Both work; which is the standard way and why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哑剧 2024-09-08 16:17:51

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

具体来说,假设:

lib.h

int Add(int a, int b);

lib.cpp

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

test.cpp

#include "lib.h"

int main() {
    Add(4);
}

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

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

lib.h

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

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

Specifically, suppose:

lib.h

int Add(int a, int b);

lib.cpp

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

test.cpp

#include "lib.h"

int main() {
    Add(4);
}

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

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

lib.h

int Add(int a, int b = 3);
扛起拖把扫天下 2024-09-08 16:17:51

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

  1. 给定参数的默认参数只能指定一次。多次指定它(即使使用相同的默认值)是非法的。

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

    具有默认

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

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

void foo(int a, int b);

为了在此类声明后调用该函数,您必须显式指定两个参数。

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

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

,从这一点开始,您可以仅使用一个显式参数来调用它。

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

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

,从这一点开始,您可以在没有显式参数的情况下调用它。

完整的示例可能如下所示

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)
{
  // ...
}

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

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

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

  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.

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.

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.

奢望 2024-09-08 16:17:51

第一种方式优于第二种方式。

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

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

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.

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.

野稚 2024-09-08 16:17:51

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

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.

海未深 2024-09-08 16:17:51

这里要记住的是,默认参数必须是函数定义中的最后一个参数。

以下代码将无法编译:

void fun(int first, int second = 10, int third);

以下代码将编译:

void fun(int first, int second, int third = 10);

On thing to remember here is that the default param must be the last param in the function definition.

Following code will not compile:

void fun(int first, int second = 10, int third);

Following code will compile:

void fun(int first, int second, int third = 10);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文