简体   繁体   English

通过价值传递或通过引用传递

[英]Passing by value or passing by reference

void Subroutine1(int Parameter1)

void Subroutine2(const int &Parameter1) 

In Subroutine1 we have to get a copy of the parameter while in Subroutine2 we don't have to make the copy, which may save some overhead. 在Subroutine1中,我们必须获得参数的副本,而在Subroutine2中我们不必进行复制,这可能会节省一些开销。

In practice Subroutine1 seems being used more often than the other. 在实践中,Subroutine1似乎比其他更频繁地使用。 Why is that the case? 为什么会这样?

In practice Subroutine1 seems being used more often than the other. 在实践中,Subroutine1似乎比其他更频繁地使用。 Why is that the case? 为什么会这样?

Because copying an int has is better over creating a reference (or pointer) and then accessing it. 因为复制int比创建引用(或指针)然后访问它更好。

More generally, all primitive types should be passed by value. 更一般地说,所有原始类型都应该通过值传递。

Because when you are dealing with primitive types (such as int ), passing by reference is actually worse performance-wise than passing by value. 因为当您处理基本类型(例如int )时,通过引用传递实际上比传递值更差 It also doesn't offer you anything. 它也没有提供任何东西。

One passes an int, the other passes a reference. 一个传递一个int,另一个传递一个引用。 As others have said, creating and accessing a reference to an int isn't much difference than just copying the int. 正如其他人所说,创建和访问int的引用与仅复制int没有多大区别。

(Edited as per correct comment) (根据正确评论编辑)

Passing by reference is (almost always) implemented by passing a pointer. 通过引用传递(几乎总是)通过传递指针来实现。 This means that, for simple types like int , the second version may be less efficient - passing a pointer has more or less the same cost as passing a simple object, and then the function needs to dereference that pointer. 这意味着,对于像int这样的简单类型,第二个版本可能效率较低 - 传递指针的成本与传递简单对象的成本大致相同,然后函数需要取消引用该指针。

For primitive data types (like int , double , char ) which are typically of smaller size, the 1st case is usually faster (accessibility) and cheaper compared to the 2nd one. 对于通常较小尺寸的原始数据类型(如intdoublechar ),第一种情况通常更快(可访问性)并且与第二种情况相比更便宜。 Remember that references are implemented more or less similar to pointers. 请记住,引用的实现或多或少类似于指针。

On side note, if Parameter1 is not going to be modified then, personally I will choose the 3rd alternative, 如果不打算修改Parameter1那么,我个人会选择第3种替代品,

void Subroutine3(const int Parameter1);

Pass-by-reference to some extent implies that the callee can mutate whatever is referenced and have that mutation take effect in the caller. 在某种程度上传递引用意味着被调用者可以改变所引用的任何内容并使该突变在调用者中生效。 If that's the case you probably want to wrap the int an object and pass that into the function by reference. 如果是这种情况,您可能希望将int包装一个对象并通过引用将其传递给函数。 This provides more explicit code. 这提供了更明确的代码。

Firstly, for int type, pass by value is faster than pass by reference. 首先,对于int类型,pass by value比通过引用传递更快。 But for your own class and struct, pass by ref is faster. 但是对于你自己的类和结构,传递ref更快。

The overhead difference between these two method for primitive types is actually very very small, you can ignore it in most cases. 这两种原始类型方法之间的开销差异实际上非常小,在大多数情况下你可以忽略它。

In my opinion, const ref of primitive type parameter is useless for normal functions. 在我看来,原始类型参数的const ref对于正常函数是没用的。 And it makes the code a little bit obscurer, so do not use it. 它使代码有点模糊,所以不要使用它。

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

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