在 C++ 中传递指针参数是按值传递吗?
C++ 中传递指针参数是按值传递吗?因为我看到对指针的任何更改都不会反映在方法之外。不过,我通过取消引用指针所做的更改已得到反映。
在这种情况下,使用指针到指针作为函数的参数来修改函数内的指针值是否可以接受/标准过程?
Is passing pointer argument, pass by value in C++? Since i see that any change to the pointer as such is not reflected outside the method. The changes i do by dereferencing the pointer is reflected though.
In that case, is it acceptable/standard procedure to use pointer to pointer as argument to a function to modify the pointer value as such within a function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
两者皆是。
指针和其他东西一样按值传递。这意味着指针变量的内容(指向的对象的地址)被复制。这意味着,如果更改函数体中指针的值,该更改将不会反映在仍指向旧对象的外部指针中。但您可以更改指向的对象的值。
如果要将指针所做的更改反映到外部指针(使其指向其他内容),则需要两级间接(指针到指针)。调用函数时,可以通过在指针名称前放置
&
来完成。这是标准的 C 做事方式。使用 C++ 时,优先使用引用而不是指针(此后也优先使用指针到指针)。
对于为什么引用应该优先于指针,有几个原因:
引用的缺点主要是:
在指针到指针的特定情况下,差异主要是简单性,但使用引用也可以很容易地删除两层指针并仅传递一个引用而不是指针到指针。
Yes to both.
Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied. That means that if you change the value of the pointer in the function body, that change will not be reflected in the external pointer that will still point to the old object. But you can change the value of the object pointed to.
If you want to reflect changes made to the pointer to the external pointer (make it point to something else), you need two levels of indirection (pointer to pointer). When calling functions it's done by putting a
&
before the name of the pointer. It is the standard C way of doing things.When using C++, using references is preferred to pointer (henceforth also to pointer to pointer).
For the why references should be preferred to pointers, there is several reasons:
Drawbacks of references are mostly:
In the specific case of pointer to pointer, the difference is mostly simplicity, but using reference it may also be easy to remove both levels of pointers and pass only one reference instead of a pointer to pointer.
我理解这里的混乱。 “按值传递”和“按引用传递”的概念虽然看起来很清楚,但并不那么清楚。
请记住,计算机不知道这些概念,并且不会按照这些概念行事。
计算机不知道这些类型。因此它不区分指针和值。
让我尝试通过示例来解释:
func1 和 func2 是相同的。两者都修改局部变量。函数从堆栈弹出后,修改就会丢失。
func3 能够更改另一个内存位置(不是函数本地的变量)。
基本上,每个函数调用都是“按值调用”。但对于指针类型,我们有一种方法可以更改内存中远程地址的内容。
I understand the confusion here. The concepts of "pass by value" and "pass by reference" are not so clear even if they seem to be so.
Bear in mind that the computer does not know these concepts and does not behave according to it.
The computer does not know about the types. Hence it does not make a distinction of pointers and values.
Let me try to explain by and example:
func1 and func2 are identical. Both modify a local variable. Modification is lost after function is popped off the stack.
func3 has ability to change another memory location (a variable which is not local to the function).
basically, every function call is "call by value". But in the case of a pointer type, we have a way to change the content of a remote address in memory.
这里,main函数中a是一个整型变量,其内容为10,地址为00F8FB04(假设)。
aptr是整型指针,存放的是整型变量a的地址,所以aptr内容就是整型变量a的地址,即00F8FB04。当我们将 aptr 作为函数参数传递时,只有 aptr 的内容(即地址)会复制到函数参数。
因此,ptr将收到aptr内容的副本(即地址00F8FB04)
Here, in main function a is an integer variable whose content is 10 and address is 00F8FB04 (assume).
aptr is pointer to integer, that store the address of integer variable a, so aptr content is address of integer variable a that is 00F8FB04. When we pass aptr as the function argument only content of aptr (that is address) are copies to function parameter.
So, ptr will receive the copy of content of aptr (that is address 00F8FB04)
如果您想更改指针本身,则可以使用指向指针的指针或对指针的引用。对于您最初的问题,从技术上讲,是的,所有参数都是按值传递的。
Either a pointer to a pointer, or a reference to a pointer, is what you would use if you wanted to potentially change the pointer itself. To your original question, technically, yes, all parameters are passed by value.
是的,确实如此,就像 C 中的情况一样。
在什么情况下?你想要什么?您可以通过
&
修饰符使用真实引用。Yes it is, as it is in C.
In which case? What do you want? You can use real references with the
&
modifier.