在 c / c++ 中何时通过引用传递以及何时通过值传递

发布于 2024-11-06 21:09:19 字数 491 浏览 4 评论 0原文

可能的重复:
它在C++ 按值传递还是按常量引用传递?
何时通过引用传递以及C++中什么时候通过指针传递?

我对 c 和 c++ 相当陌生,关于向函数传递参数我有以下问题,我认为最好在这里问并直接从知识来源获取知识大师们。

什么时候应该按值传递参数,什么时候应该按引用传递参数?现实世界中最常用的方法是什么?

提前致谢。

Possible Duplicate:
Is it better in C++ to pass by value or pass by constant reference?
When to pass by reference and when to pass by pointer in C++?

I am fairly new to c and c++ and I have the following question with regards to passing parameters to functions and thought it best to ask here and come straight to the source of the knowledge from the gurus.

When should I pass parameters by value and when sould they be passed by reference? And what is the most commonly used method in the real world?

Thanks in advance.

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

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

发布评论

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

评论(6

半暖夏伤 2024-11-13 21:09:19

更喜欢通过引用传递,通常是以下一项或多项:

  1. 如果它很大。 (如果它是const,那么一定要这样做,但无论如何都要经常这样做,具体取决于此列表的其余部分)
  2. 如果您想编辑它并使更改全局可见。
  3. 如果您希望虚拟函数具有多态性。
  4. 在模板代码中,如果您不知道它将是什么类型。

按值传递:

  1. 如果它是一个简单的内置类型(例如 int),
  2. 如果您不希望更改改变原始对象。
  3. 如果您希望能够传递临时变量而不必使其成为常量。

Prefer pass by reference, usually one or more of:

  1. If it's big. (Definitely do this if it's const, do it anyway quite often though, depending on the rest of this list)
  2. If you want to edit it and make changes globally visible.
  3. If you want virtual functions to behave polymorphicly.
  4. In template code if you don't know what type(s) it's going to be.

Pass by value:

  1. If it's a trivial builtin type (e.g. int)
  2. If you don't want changes to be alter the original object.
  3. If you want to be able to pass temporaries in without having to make it const.
末蓝 2024-11-13 21:09:19

如果参数大于寄存器并且您不修改它,则应该将其作为 const 引用传递。当然,在某些特殊情况下,您稍后会创建一个副本左右,但一般来说这条规则是成立的。

对于较小的类型,应该没有关系(即 const int& 或 int)应该给出相同的结果。为了安全起见,无论如何都要按值传递它们。对于通用代码,您可以使用 Boost 调用特征自动决定是否使用按引用或按值的库。

如果要修改对象,将其作为引用传入可以让您就地修改它,这通常比返回新对象并覆盖原始对象要快。

If the parameter is bigger than register and you're not modifying it, you should pass it as const reference. Of course, there are exceptional cases where you later create a copy or so, but in general this rule holds true.

For smaller types, it should not matter (i.e. const int& or int) should give the same result. To be safe, pass them by value anyway. For generic code, you can use the Boost Call Traits library which automatically decides whether to use by-reference or by-value.

If you want to modify the object, passing it in as reference allows you to modify it in-place, which is usually faster than returning a new one and overwriting the original one.

停滞 2024-11-13 21:09:19

默认情况下,通过 const 引用传递,但以下情况除外:

1) 如果您想要一个 in/out 或 out 参数 - 一个由函数更改的参数。在这种情况下,请使用非常量引用。
2) 如果要传递整数、浮点数或指针 - 按值传递。

By default, pass by const reference, except:

1) If you want an in/out or out parameter - one that gets changed by the function. In that case, use a non-const reference.
2) If you are passing an integer, float or pointer - pass by value.

从来不烧饼 2024-11-13 21:09:19

通常,当您需要更改参数并且不希望调用者必须进行复制时,应该使用按值传递。您应该在其他任何地方都使用 const 的引用传递。

然而,这实际上归结为“C++ 作为语言联盟”——例如,如果您正在编写 STL 算法,则需要使用按值传递,因为迭代器和函子近似于指针和函数指针。

Generally, you should use pass by value when you need to change an argument, and don't want the caller to have to make copies. You should use pass by reference to const pretty much everywhere else.

However, this really comes down to the "C++ as a federation of languages" bit -- for example, if you're writing STL algorithms you need to use pass by value, because iterators and functors approximate pointers and function pointers.

旧人哭 2024-11-13 21:09:19

除其他事项外,这在很大程度上取决于您的需求。如果您希望看到对传递的变量所做的更改反映在当前函数中,那么您肯定会选择按引用传递,但如果您严格要求对传递的变量进行任何更改变量未反映在您当前的函数中,请选择“按值传递”。

谢谢,
马维亚

It depends ,among other things,largely upon your needs.If you want to see the changes done to passed variable reflected in your current function,surely you will go with pass by reference but if you are strict that any changes to done to the passed variable is not reflected in your current function go for 'passed by value'.

Thanks,
Mawia

蓝天 2024-11-13 21:09:19

按值传递,除非您有理由不这样做。不这样做的原因:

  1. 您希望函数以某种方式更改对象。通过引用或指针传递。
  2. 物体“大”。 “大”的精确定义将取决于您的计算机、平台、复制对象的后果等。您必须根据具体情况进行评估。通过 const 引用传递。
  3. 制作对象的副本会做一些“坏事”。例如,如果通过复制文件指针对象,它会创建另一个文件,或者通过复制同步对象,它会创建死锁。通过 const 引用传递。
  4. 您需要传入对象的多态行为。通过引用或指针传递。按值传递将对对象进行切片

请注意,对于上面的#2,即使对象很大,您可能仍然希望按值传递。按值传递更简单,因此更容易维护,除非您有理由相信按值传递会在代码中产生实际问题,否则按 const 引用传递可能是不成熟的微优化。例如,如果您的函数采用 64 字节(甚至 64k 字节)的参数,但该函数每天仅调用一次,则实际上没有理由传递 const 引用。它几乎没有为你节省任何东西。首先在发布模式下分析代码,然后进行优化。

Pass by value unless you have a reason not to. Reasons not to:

  1. You want the function to change the object in some way. Pass by reference or by pointer.
  2. The object is "big." The precise definition of "big" will depend on your computer, your platform, the ramifications of making a copy of your object, etc. You will have to evaluate on a case-by-case basis. Pass by const reference.
  3. Making a copy of the object would do something "bad". For example, if by copying a file pointer object it creates another file, or by copying a synchronization object it creates a deadlock. Pass by const reference.
  4. You need polymorphic behavior on the passed-in object. Pass by reference or by pointer. Passing by value will slice the object.

Note that with #2 above, even if the object is big you may still want to pass by value. Passing by value is simpler therefore easier to maintain, and unless you have reason to believe that passing by value is creating an actual problem in your code, passing by const reference might be premature micro optiminzation. For example, if your function takes a parameter that is 64 bytes (or even 64k bytes) but that function is only called once a day, there's really no reason to pass by const reference. It saves you virtually nothing. Profile your code in release mode first, then optimize.

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