c++ - 为什么在 C++ 中使用按值调用?

标签 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/

相关文章:

c++ - 为什么 set::find 不是模板?

c++ - std 或 boost 中是否有类似循环预付的东西?

c++ - OpenGL调试错误回调函数的ID参数是什么?

C++ Windows 添加选项到菜单

c++ - 用C++下载文件的最简单的库是什么

abap - EXPORTING方法参数如何按值传递?

java - 重新排列 ArrayList 的顺序