简体   繁体   English

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

[英]Initializing variable in C++ function header

I've come across some C++ code that looks like this (simplified for this post): 我遇到过一些看起来像这样的C ++代码(本文简化):

(Here's the function prototype located in someCode.hpp ) (这是位于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 ) (这是函数体的第一行,位于someCode.cpp#includesomeCode.hpp

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

Can I legally call someFunction using: 我可以合法地调用someFunction

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? 变量ptr1ptr2valval2是否已经适当定义, val1val2不等于零? Why or why not? 为什么或者为什么不?

And if it is legal, is this syntax preferred vs overloading a function to account for the optional parameters? 如果它是合法的,这种语法是否优先于重载函数以考虑可选参数?

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. 关于const的注释,它不适用于默认值本身,它适用于参数。 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). 如果你有一个类型为const char* fruit = "apple" ,那并不意味着必须使用一个字符指针调用它的值与"apple"字符串文字的地址相同(这很好,因为这很难保证)。 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. 它只是意味着必须使用指向常量字符的指针调用它,并告诉您被调用的函数不需要写入该内存,只能从中读取。

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. 我个人更喜欢默认参数。

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. 默认值是接口的一部分,更改可能会破坏客户端(如@Juraj已经注意到的)此外,Overloads可以更轻松地添加其他(组合)参数,而不会破坏(二进制)接口。

Overloads are resolved at compile time, which can give the compiler better optimization (esp inlining) possibilities. 在编译时解决重载,这可以为编译器提供更好的优化(esp inlining)可能性。 eg 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. 使用重载可能更好。

Can I legally call someFunction using: 我可以合法地调用someFunction:

someFunction(*ptr1, *ptr2); 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. 是的,函数接受的其他2个变量将具有您在头文件中设置的默认值,两个参数均为零。

But if you do supply the 3rd and the 4th argument to the function, then those values are considered instead of the default values. 但是如果确实为函数提供了第3个和第4个参数,则会考虑这些值而不是默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM