c++按值或参考传递对象?
一个简单的问题,我在这里找不到答案。
我知道的是,在通话过程中将参数传递给函数时,例如
void myFunction(type myVariable)
{
}
void main()
{
myFunction(myVariable);
}
int
,float
等简单数据类型等。该函数按值调用。
但是,如果myvariable
是一个数组,则仅传递起始地址(即使我们的函数是值函数的调用)。
如果myvariable
是对象,则仅传递对象的地址而不是创建副本并传递。
回到问题。 C ++是通过参考还是值传递对象?
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.
But if myVariable
is an array, only the starting address is passed (even though our function is a call by value function).
If myVariable
is an object, also only the address of the object is passed rather than creating a copy and passing it.
So back to the question. Does C++ pass a object by reference or value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
参数是按值传递的,除非函数签名另有指定:
void foo(type arg)
,arg
arg 都按值传递,无论type> type> type> type 是一种简单的类型,一种指针类型或类型类型,
void foo(type& arg)
,arg
通过参考将传递。如果存在数组,则传递的值是指向数组的第一个元素的指针。如果您知道编译时数组的大小,则也可以通过参考传递数组:
void foo(type(& arg)[10] [10])
。Arguments are passed by value, unless the function signature specifies otherwise:
void foo(type arg)
,arg
is passed by value regardless of whethertype
is a simple type, a pointer type or a class type,void foo(type& arg)
,arg
is passed by reference.In case of arrays, the value that is passed is a pointer to the first element 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])
.C ++始终为您提供选择: ALL 类型
t
(数组除外,请参见下文)可以通过进行值来传递参数类型t
,并通过引用通过参数类型t&
,引用到t
。当没有明确注释参数类型为参考(
type& myvariable
)时,无论特定类型如何,它都是按值传递的始终。对于用户定义的类型(这就是复制构造函数的目的)。同样对于指针,即使复制指针也不会复制指向的内容。阵列更复杂。数组不能按值,参数类型,例如
int arr []
实际上只是int *arr *arr
的语法。这不是传递到从数组中产生指针的函数的行为,几乎所有可能的操作(不包括少数几个sizeof
)做。一个 can 通过参考到达阵列,但明确注释为参考:int(& myArray)[100] [100]
(请注意ampersand)。C++ always gives you the choice: All types
T
(except arrays, see below) can be passed by value by making the parameter typeT
, and passed by reference by making the parameter typeT &
, reference-to-T
.When the parameter type is not explicitly annotated to be a reference (
type &myVariable
), it is always passed 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.Arrays are a bit more complicated. Arrays cannot be passed by value, parameter types like
int arr[]
are really just different syntax forint *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 likesizeof
) does that. One can pass a reference-to-an-array, but this explicitly annotated as reference:int (&myArray)[100]
(note the ampersand).C ++既可以通过价值来通过,又可以通过参考范例传递。
您可以在下面找到两个示例用法。
http://wwwww.learlncpp.com/cpp-tutorial /72-passing-arguments-by-value/
http://www.lealencpp.com/cpp-tutorial/73-passing-arguments-by-repery/
阵列是特殊的构造,当您将数组作为参数传递时,第一个元素作为值传递给数组中的元素类型。
当您将指针作为参数传递时,实际上是通过参考范式(如C)实现通行证,因为当您在指定地址中修改数据时,您可以在呼叫者函数中精确修改对象。
C++ makes both pass by value and pass by reference paradigms possible.
You can find two example usages below.
http://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
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.
上面已经有非常有用的答案。我只是在以下内容中分享一个测试案例,希望未来的读者能发现它有帮助:
这是输出:
最后, :
,即使您不明确地通过对其进行明确传递,您对函数中数组进行的任何更改都会突变原始数组。
如果您想将数组传递到函数中并保护其免受突变,则可以手动制作其副本并将副本传递给该功能。另外,您可以使用包装数组的容器,例如
std ::数组
和std :: vector
。 [source]There are already very informative answers above. I am just sharing a test case in the following in hope that the future readers may find it helpful:
Here is the output:
Finally, from this:
That is, any changes you make to array within the function mutates the original array even if you don't explicitly pass in a reference to it.
In case, you want to pass an array into a function and protect it from mutations, you may manually make a copy of it and pass the copy to the function. Alternatively, you may use containers wrapping an array, e.g
std::array
andstd::vector
. [Source]在C ++中,称为类,结构或联合的类型被认为是“类型的”。这些是按值传递的,或者您可以说使用复制构造函数的副本传递给函数。当我们实现二进制树时,这很明显,其中您几乎总是在作用于二进制树上的递归函数中具有节点 *类型的参数。这是为了促进该节点的修改。如果要按原样传递节点(即不是指针类型),则对节点的修改将是本地副本。即使在向量的情况下,传递向量的副本也将传递给函数,以避免使用参考。
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 ++通过值(按值)的参数(int*)或引用(int&)。您无法修改调用函数中调用块的VAR。阵列是指针。
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.