C++ 是按值还是按引用传递对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21215409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does C++ pass objects by value or reference?
提问by user3041058
A simple question for which I couldn't find the answer here.
一个简单的问题,我在这里找不到答案。
What I understand is that while passing an argument to a function during call, e.g.
我的理解是,在调用过程中将参数传递给函数时,例如
void myFunction(type myVariable)
{
}
void main()
{
myFunction(myVariable);
}
For simple datatypes like int
, float
, etc. the function is called by value.
对于像int
、float
等简单的数据类型,函数是按值调用的。
But if myVariable
is an array, only the starting address is passed (even though our function is a call by value function).
但是如果myVariable
是数组,则只传递起始地址(即使我们的函数是按值调用的函数)。
If myVariable
is an object, also only the address of the object is passed rather than creating a copy and passing it.
如果myVariable
是对象,也只传递对象的地址,而不是创建副本并传递它。
So back to the question. Does C++ pass a object by reference or value?
所以回到问题。C++ 是否通过引用或值传递对象?
采纳答案by Oswald
Arguments are passed by value, unless the function signature specifies otherwise:
参数按值传递,除非函数签名另有说明:
- in
void foo(type arg)
,arg
is passed by value regardless of whethertype
is a simple type, a pointer type or a class type, - in
void foo(type& arg)
,arg
is passed by reference.
- in
void foo(type arg)
,arg
无论type
是简单类型、指针类型还是类类型,都按值传递, - in
void foo(type& arg)
,arg
通过引用传递。
In case of arrays, the value that is passed is a pointer to the first elements of the array. If you know the size of the array at compile time, you can pass an array by reference as well: void foo(type (&arg)[10])
.
对于数组,传递的值是指向数组第一个元素的指针。如果你知道在编译时数组的大小,你可以通过引用传递一个阵列,以及:void foo(type (&arg)[10])
。
回答by Oswald
C++ always gives you the choice: Alltypes T
(except arrays, see below) can be passed by value by making the parameter type T
, and passed by reference by making the parameter type T &
, reference-to-T
.
C++ 总是为您提供选择:所有类型T
(数组除外,见下文)都可以通过设置参数 type 来按值传递,并可以通过设置参数 type 来T
按引用传递T &
, reference-to- T
。
When the parameter type is not explicitly annotated to be a reference (type &myVariable
), it is alwayspassed by value regardless of the specific type. For user-defined types too (that's what the copy constructor is for). Also for pointers, even though copying a pointer does not copy what's pointed at.
当参数类型没有显式标注为引用(type &myVariable
)时,无论具体类型如何,它总是按值传递。对于用户定义的类型也是如此(这就是复制构造函数的用途)。同样对于指针,即使复制指针不会复制指向的内容。
Arrays are a bit more complicated. Arrays cannot be passed by value, parametertypes like int arr[]
are really just different syntax for int *arr
. It's not the act of passing to a function which produces a pointer from an array, virtually every possible operation (excluding only a few ones like sizeof
) does that. One canpass a reference-to-an-array, but this explicitly annotated as reference: int (&myArray)[100]
(note the ampersand).
数组有点复杂。数组不能按值传递,参数类型int arr[]
实际上只是int *arr
. 这不是传递给从数组生成指针的函数的行为,几乎所有可能的操作(仅排除像 那样的少数操作sizeof
)都会这样做。一个可以通过一个参考到一个阵列,但是这明确地标注为参考:int (&myArray)[100]
(注意号)。
回答by mcvkr
C++ makes both pass by value and pass by reference paradigms possible.
C++ 使传值和传引用范式成为可能。
You can find two example usages below.
您可以在下面找到两个示例用法。
http://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
http://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/
Arrays are special constructs, when you pass an array as parameter, a pointer to the address of the first element is passed as value with the type of element in the array.
数组是特殊的构造,当您将数组作为参数传递时,指向第一个元素地址的指针将作为值传递给数组中元素的类型。
When you pass a pointer as parameter, you actually implement the pass by reference paradigm yourself, as in C. Because when you modify the data in the specified address, you exactly modify the object in the caller function.
当您将指针作为参数传递时,您实际上自己实现了引用传递范式,就像在 C 中一样。因为当您修改指定地址中的数据时,您正是在调用者函数中修改对象。
回答by Piyush Kundnani
In C++, types declared as a class, struct, or union are considered "of class type". These are passed by value or you can say a copy using copy constructor is passed to the functions. This is pretty evident when we implement binary trees wherein you almost always have a Node * type of param in the recursive function acting on the binary tree. This is so as to facilitate modification of that node. If the node were to be passed as is (i.e not being a pointer type), the modifications to the nodes would have been to the local copy. Even in the case of vectors, while passing a copy of vectors is passed to the functions, to avoid which we use a reference &.
在 C++ 中,声明为类、结构或联合的类型被视为“属于类类型”。这些是按值传递的,或者您可以说使用复制构造函数的副本传递给函数。当我们实现二叉树时,这一点非常明显,其中在作用于二叉树的递归函数中几乎总是有一个 Node * 类型的参数。这是为了便于修改该节点。如果节点按原样传递(即不是指针类型),则对节点的修改将是对本地副本的修改。即使在向量的情况下,在将向量的副本传递给函数时,我们也会使用引用 & 来避免这种情况。
回答by scraatz
C++ passes arguments that are no pointers (int*) or references (int&) by value. You cannot modify the var of the calling block in the called function. Arrays are pointers.
C++ 通过值传递没有指针 (int*) 或引用 (int&) 的参数。您不能在被调用函数中修改调用块的 var。数组是指针。