简体   繁体   English

通过引用传递时,是否应在函数中保留相同的变量名?

[英]When passing by reference, should you keep the same variable name in the function?

When passing variables by reference, should you keep the same variable name in the function? 通过引用传递变量时,您应该在函数中保留相同的变量名吗? Eg 例如

int main()
{ 
const int Players = 1000;
array <double, Players> PlayerStrengths;
}

template <typename T, size_t Size>
void GeneratePlayerStrengths(array <T, Size>& _PlayerStrengths) 
{
//-- Logic here --
}

No. They may have different names. 否。它们的名称可能不同。

Also, if you start a name with an underscore followed by an uppercase letter, the behavior is undefined. 另外,如果您使用带下划线的大写字母开头的名称,则该行为是不确定的。 Don't do that. 不要那样做

When passing variables by reference, should you keep the same variable name in the function? 通过引用传递变量时,您应该在函数中保留相同的变量名吗?

In the general case no. 在一般情况下,没有 Or at least there is no reason why they should. 或至少没有理由这么做。

In fact I would suggest you might want to approach the issue from the other way, and say 实际上,我建议您可能要以其他方式处理该问题,然后说

"should the calling function use variable names that are the same as the parameter name in functions it is calling?" “调用函数应该使用与调用函数中的参数名称相同的变量名称吗?”

In this case the answer is maybe 在这种情况下,答案可能是

For instance, if an api to send a message takes a parameter named msg, you might want your calling function to use the same variable name 例如,如果要发送消息的api使用名为msg的参数,则您可能希望您的调用函数使用相同的变量名

However, the important rule is that the variable name inside the function you are calling should make sense in the context of that function, and the variable name in the calling function should make sense in that context. 然而,最重要的规则是,你在呼唤应该是有意义的功能的情况下的功能,以及在调用函数的变量名中的变量名应该是有意义在这方面。

For instance: 例如:

int SendMsg( Msg& msgToSend)
{
// Serialise and transmit message

  return OK;
}


void GenerateAndSendShutdown (void)
{
   //You could call this msgToSend, but it wouldnt make the code any clearer
   Msg shutdownCmd = "Shutdown";
   SendMsg (shutdownCmd);
}

In your particular case, I would say it would make sense if they were the same. 在您的特定情况下,我想说如果它们相同的话就很有意义。

Variable passed are of two types: formal arguments (as seen as function arguments) eg void func(int& formalarg) and actual arguments (as arguments passed while calling a function), eg func(actualarg), where actualarg may or maynot be of integer type. 传递的变量有两种类型:形式参数(如视为函数参数),例如void func(int&formalarg)和实际参数(如调用函数时传递的参数),例如func(actualarg),其中,aractarg可以是整数,也可以不是整数类型。 In case the typese differ, there has to be cast operator available from other type of actual argument to int (expected formal argument type). 如果类型不同,则必须将强制转换运算符从其他实际参数类型转换为int(期望的形式参数类型)。 If two types are same, for program readability keeping the same name makes the program more elegant to be understood about the purpose provided you follow a meaningful naming convention. 如果两种类型相同,则为了使程序易于阅读,保持相同的名称可以使程序在遵循有意义的命名约定的前提下更加优雅地被理解。 Hope this helps. 希望这可以帮助。

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

相关问题 在将派生类对象传递给C ++中具有基类参数的函数时,为什么要传递引用而不是值? - Why should you pass a reference instead of a value when passing a derived class object to a function with a base class parameter in C++? 将变量 NAME 传递给 function - Passing variable NAME to a function Boost Python:在函数中通过引用传递变量时出错 - Boost Python: Error when passing variable by reference in a function 通过引用递归函数传递变量 - Passing a variable by reference to a recurring function 将变量传递给函数以将数组引用到 - Passing a variable to function for reference an array into 如果通过引用传递变量,则冗余 mov 操作 - Redundant mov operations if you passing variable by reference 您应该通过引用还是使用临时变量重载“=”运算符? - Should you overload the "=" operator by reference or with a temporary variable? 通过引用传递变量时出现“未定义的引用”错误 - “Undefined reference” error when passing variable by reference 将没有名称的变量传递给函数 - passing variable without a name to function 通过引用 function 传递变量使得 function 内联 - Is passing a variable by reference to a function makes the function inline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM