<分区>
当使用按引用调用更有效时,将参数作为按值调用的目的是什么? (对于非原始数据类型。)
此外,如果将 const 标记添加到按引用调用的参数中,这样它们就不会被修改怎么办?
是否存在使用按值调用而不是按引用调用有益的情况?
标签 c++ call-by-value
<分区>
当使用按引用调用更有效时,将参数作为按值调用的目的是什么? (对于非原始数据类型。)
此外,如果将 const 标记添加到按引用调用的参数中,这样它们就不会被修改怎么办?
是否存在使用按值调用而不是按引用调用有益的情况?
最佳答案
我保存的摘录,您可能会觉得有用(来自 CodingUnitTutorials )
There are two possible reasons to not use call-by-reference: side effects and privacy. Unwanted side effects are usually caused by inadvertently changes that are made to a call by reference parameter. Also in most cases you want the data to be private and that someone calling a function only be able to change if you want it. So it is better to use a call by value by default and only use call by reference if data changes are expected.
关于c++ - 为什么在 C++ 中使用按值调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28415931/