功能参数的默认值

发布于 2025-01-31 10:49:38 字数 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

塔塔猫 2025-02-07 10:49:38

如果将声明放在标题文件中,将定义放在单独的.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);
‖放下 2025-02-07 10:49:38

在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.

有深☉意 2025-02-07 10:49:38

第一种方法是第二种方法。

这是因为标题文件将显示参数是可选的,并且其默认值将是什么。此外,无论实现相应的.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.

清风不识月 2025-02-07 10:49:38

默认参数必须在函数原型中的第一次出现函数名称的第一次出现。如果省略了函数原型,因为函数定义也用作原型,则默认参数应在函数标题中指定。

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.

北凤男飞 2025-02-07 10:49:38

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

以下代码不会编译:

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 和您的相关数据。
原文