在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更喜欢通过引用传递,通常是以下一项或多项:
const
,那么一定要这样做,但无论如何都要经常这样做,具体取决于此列表的其余部分)按值传递:
Prefer pass by reference, usually one or more of:
const
, do it anyway quite often though, depending on the rest of this list)Pass by value:
如果参数大于寄存器并且您不修改它,则应该将其作为 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.
默认情况下,通过 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.
通常,当您需要更改参数并且不希望调用者必须进行复制时,应该使用按值传递。您应该在其他任何地方都使用 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.
除其他事项外,这在很大程度上取决于您的需求。如果您希望看到对传递的变量所做的更改反映在当前函数中,那么您肯定会选择按引用传递,但如果您严格要求对传递的变量进行任何更改变量未反映在您当前的函数中,请选择“按值传递”。
谢谢,
马维亚
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
按值传递,除非您有理由不这样做。不这样做的原因:
请注意,对于上面的#2,即使对象很大,您可能仍然希望按值传递。按值传递更简单,因此更容易维护,除非您有理由相信按值传递会在代码中产生实际问题,否则按 const 引用传递可能是不成熟的微优化。例如,如果您的函数采用 64 字节(甚至 64k 字节)的参数,但该函数每天仅调用一次,则实际上没有理由传递 const 引用。它几乎没有为你节省任何东西。首先在发布模式下分析代码,然后进行优化。
Pass by value unless you have a reason not to. Reasons not to:
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.