按值传递或按引用传递

发布于 2024-12-20 08:53:33 字数 211 浏览 1 评论 0原文

void Subroutine1(int Parameter1)

void Subroutine2(const int &Parameter1) 

在 Subroutine1 中我们必须获取参数的副本,而在 Subroutine2 中我们不必复制,这可能会节省一些开销。

在实践中,子例程 1 似乎比另一个更常用。为什么会这样呢?

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.

In practice Subroutine1 seems being used more often than the other. Why is that the case?

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

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

发布评论

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

评论(7

任性一次 2024-12-27 08:53:33

实际上,子例程 1 似乎比另一个更常用。
为什么会这样?

因为复制 int 比创建引用(或指针)然后访问它更好。

更一般地,所有原始类型都应该按值传递。

In practice Subroutine1 seems being used more often than the other.
Why is that the case?

Because copying an int has is better over creating a reference (or pointer) and then accessing it.

More generally, all primitive types should be passed by value.

殤城〤 2024-12-27 08:53:33

因为当您处理基本类型(例如 int)时,按引用传递实际上比按值传递更差性能。它也不会为您提供任何东西。

Because when you are dealing with primitive types (such as int), passing by reference is actually worse performance-wise than passing by value. It also doesn't offer you anything.

山田美奈子 2024-12-27 08:53:33

一个传递一个 int,另一个传递一个引用。正如其他人所说,创建和访问 int 的引用与复制 int 没有太大区别。

(根据正确评论进行编辑)

One passes an int, the other passes a reference. As others have said, creating and accessing a reference to an int isn't much difference than just copying the int.

(Edited as per correct comment)

赏烟花じ飞满天 2024-12-27 08:53:33

通过引用传递(几乎总是)通过传递指针来实现。这意味着,对于像 int 这样的简单类型,第二个版本可能效率较低 - 传递指针与传递简单对象的成本或多或少相同,然后函数需要取消引用该指针。

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.

有木有妳兜一样 2024-12-27 08:53:33

对于通常尺寸较小的原始数据类型(例如 intdoublechar),第一种情况通常更快(可访问性)并且比2号便宜。请记住,引用的实现或多或少类似于指针。

顺便说一句,如果 Parameter1 不会被修改,那么我个人会选择第三种选择,

void Subroutine3(const int Parameter1);

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

void Subroutine3(const int Parameter1);
未央 2024-12-27 08:53:33

在某种程度上,引用传递意味着被调用者可以改变所引用的任何内容,并使该改变在调用者中生效。如果是这种情况,您可能希望将 int 包装为一个对象,并通过引用将其传递到函数中。这提供了更明确的代码。

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. This provides more explicit code.

北渚 2024-12-27 08:53:33

首先,对于int类型,按值传递比按引用传递更快。但对于您自己的类和结构,通过 ref 传递会更快。

这两种方法对于基本类型的开销差异实际上非常非常小,大多数情况下你可以忽略它。

在我看来,原始类型参数的 const ref 对于普通函数来说是没有用的。而且它使代码有点模糊,所以不要使用它。

Firstly, for int type, pass by value is faster than pass by reference. But for your own class and struct, pass by ref is faster.

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. And it makes the code a little bit obscurer, so do not use it.

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