在 C++ 中初始化变量函数头

发布于 2024-11-07 04:07:35 字数 823 浏览 1 评论 0原文

我遇到过一些看起来像这样的 C++ 代码(针对本文进行了简化):(

这是位于 someCode.hpp 中的函数原型)

void someFunction(const double & a, double & b, const double c = 0, const double * d = 0);

(这是位于 someCode.hpp 中的函数体的第一行>someCode.cpp#includesomeCode.hpp

void someFunction(const double & a, double & b, const double c, const double * d);

我可以合法地调用 someFunction 使用:

someFunction(*ptr1, *ptr2);

/或

someFunction(*ptr1, *ptr2, val1, &val2);

和 变量 ptr1ptr2valval2 已正确定义,且 val1val2 不等于零?为什么或为什么不呢?

如果它是合法的,那么与重载函数以考虑可选参数相比,这种语法是否是首选?

I've come across some C++ code that looks like this (simplified for this post):

(Here's the function prototype located in someCode.hpp)

void someFunction(const double & a, double & b, const double c = 0, const double * d = 0);

(Here's the first line of the function body located in someCode.cpp that #include's someCode.hpp)

void someFunction(const double & a, double & b, const double c, const double * d);

Can I legally call someFunction using:

someFunction(*ptr1, *ptr2);

and/or

someFunction(*ptr1, *ptr2, val1, &val2);

where the variables ptr1, ptr2, val, and val2 have been defined appropriately and val1 and val2 do not equal zero? Why or why not?

And if it is legal, is this syntax preferred vs overloading a function to account for the optional parameters?

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

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

发布评论

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

评论(4

欢你一世 2024-11-14 04:07:35

是的,这是合法的,这称为默认参数。我想说,由于涉及更少的代码,它比重载更可取,是的。

关于您对 const 的评论,这不适用于默认值本身,它适用于参数。如果您有一个 const char*fruit = "apple" 类型的参数,这并不意味着必须使用值与 的地址相同的字符指针来调用它“apple” 字符串文字(这很好,因为这很难保证)。它只是意味着必须使用指向常量字符的指针来调用它,并告诉您被调用的函数不需要写入该内存,而只需从中读取。

Yes, this is legal, this is called default arguments. I would say it's preferred to overloading due to involving less code, yes.

Regarding your comment about const, that doesn't apply to the default value itself, it applies to the argument. If you have an argument of type const char* fruit = "apple", that doesn't mean it has to be called with a character pointer whose value is the same as the address of the "apple" string literal (which is good, since that would be hard to guarantee). It just means that it has to be called with a pointer to constant characters, and tells you that the function being called doesn't need to write to that memory, it is only read from.

寻梦旅人 2024-11-14 04:07:35

是的,这些参数是可选的,当您不传递它们时,将使用给定的默认值。

使用默认参数值而不是重载有一些优点和缺点。优点是接口和实现部分的输入都较少。但缺点是默认值是接口的一部分及其所有后果。然后,当您更改默认值时,例如在使用重载时需要重新编译大量代码而不是单个文件。

我个人更喜欢默认参数。

Yes, the parameters are optional and when you don't pass them, the given default values will be used.

It has some advantages and disadvantages to use default parameter values instead of overloading. The advantage is less typing in both interface and implementation part. But the disadvantage is that the default value is a part of interface with all its consequences. Then when you change the default value, you for example need to recompile a lot of code instead of a single file when using overloading.

I personally prefer default parameters.

烂人 2024-11-14 04:07:35

我想扩展一下默认参数是否优先于重载。

通常它们是出于其他答案中给出的所有原因,最明显的是更少的样板代码。

在某些情况下,还有一些充分的理由使重载成为更好的选择:

默认值是界面的一部分,更改可能会破坏客户端(正如 @Juraj 已经指出的那样)
此外,重载可以更轻松地添加其他参数(组合),而不会破坏(二进制)接口。

重载在编译时解决,这可以为编译器提供更好的优化(特别是内联)可能性。例如,如果您有这样的事情:

void foo(Something* param = 0) {
   if (param == 0) {
      simpleAlgorithm();
   } else {
      complexAlgorithm(param);
   }
}

使用重载可能会更好。

I'd like to expand a bit on whether Default Parameters are preferred over overloading.

Usually they are for all the reasons given in the other answers, most notably less boilerplate code.

There are also valid reasons that make overloading a better alternative in some situations:

Default values are part of the interface, changes might break clients (as @Juraj already noted)
Additionally Overloads make it easier to add additional (combinations of) parameters, without breaking the (binary) interface.

Overloads are resolved at compile time, which can give the compiler better optimization (esp inlining) possibilities. e.g. if you have something like this:

void foo(Something* param = 0) {
   if (param == 0) {
      simpleAlgorithm();
   } else {
      complexAlgorithm(param);
   }
}

It might be better to use overloads.

埋情葬爱 2024-11-14 04:07:35

我可以合法地调用 someFunction 使用:

someFunction(*ptr1, *ptr2);

绝对地!是的,该函数接受的其他 2 个变量将具有您在头文件中设置的默认值,这两个参数都为零。

但是,如果您确实向函数提供了第三个和第四个参数,那么将考虑这些值而不是默认值。

Can I legally call someFunction using:

someFunction(*ptr1, *ptr2);

Absolutely! Yes, the other 2 variables that the function accepts would have default values you have set in the header file which is zero for both the arguments.

But if you do supply the 3rd and the 4th argument to the function, then those values are considered instead of the default values.

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