在 C++ 中传递指针参数,按值传递吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4426474/
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
Is passing pointer argument, pass by value in C++?
提问by Sulla
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.
在 C++ 中传递指针参数,按值传递吗?因为我看到对指针的任何更改都不会反映在方法之外。我通过取消引用指针所做的更改会得到反映。
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?
在这种情况下,使用指向指针的指针作为函数的参数来修改函数内的指针值是否可以接受/标准过程?
回答by kriss
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.
如果要反映对外部指针的指针所做的更改(使其指向其他内容),则需要两个间接级别(指向指针的指针)。调用函数时,它是通过&
在指针名称之前放置 a 来完成的。这是标准的 C 语言做事方式。
When using C++, using references is preferred to pointer (henceforth also to pointer to pointer).
使用 C++ 时,使用引用优于指针(此后也使用指向指针的指针)。
For the whyreferences should be preferred to pointers, there is several reasons:
对于为什么引用应首选指针,有几个原因:
- references introduce less syntaxic noise than pointers in function body
- references keep more informations than pointers, than can be useful for compiler
- 引用比函数体中的指针引入更少的语法噪音
- 引用保留比指针更多的信息,比对编译器有用
Drawbacks of references are mostly:
引用的缺点主要是:
- they break the simple pass-by-value rule of C, what makes understanding the behavior of a function regarding of parameters (will they be changed ?) less obvious. You also need function prototype to be sure. But that is not really worse than the multiple pointer levels necessary when using C.
- they are not supported by C, that can be a problem when you write code that should work with both C and C++ programs (but that's not the most usual case).
- 它们打破了 C 语言的简单按值传递规则,是什么让理解函数关于参数的行为(它们会被改变吗?)不太明显。您还需要函数原型来确定。但这并不比使用 C 时所需的多指针级别更糟糕。
- C 不支持它们,当您编写的代码应该同时适用于 C 和 C++ 程序时,这可能是一个问题(但这不是最常见的情况)。
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.
在指针到指针的特定情况下,区别主要是简单,但使用引用也可能很容易删除两级指针并只传递一个引用而不是指向指针的指针。
回答by Atilla Baspinar
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:
我理解这里的混乱。“按值传递”和“按引用传递”的概念即使看起来如此,也不是那么清楚。请记住,计算机不知道这些概念,也不会根据这些概念运行。计算机不知道这些类型。因此它不区分指针和值。让我试着用例子来解释:
void func1(int x)
{
x = 5;
}
void func2(int *x)
{
int a;
x = &a;
}
The operation is same for the machine in both functions: It gets the argument and changes it. Note that, in second function it does not modifiy *x, it modifies x.
机器在两个函数中的操作是相同的:它获取参数并更改它。请注意,在第二个函数中,它不会修改 *x,而是修改 x。
Now if we call these functions,
现在如果我们调用这些函数
int y = 10;
func1(y); //value of y does not change
func2(&y); //value of &y does not change, but the value of the address which y points may change.
I prefer to say that, 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 another address in memory.
我更愿意说,基本上,每个函数调用都是“按值调用”。但是在指针类型的情况下,我们有办法改变内存中另一个地址的内容。
If we had written func2
as
如果我们写func2
的
void func2(int *x)
{
*x = 5;
}
Then, this would be a real case of "call by reference".
那么,这将是“按引用调用”的真实案例。
回答by Umair Mubasher
Pass by value using PointersI'll explain it by example:
使用指针按值传递我将通过示例进行解释:
void f(int *ptr)
{
cout<<*ptr;
}
int main ()
{
int a=10;
int *aptr=&a;
f(aptr);
return 0;
}
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)
这里,在主函数中,a 是一个整数变量,其内容为 10,地址为 00F8FB04(假设)。aptr是整型指针,存放整型变量a的地址,所以aptr内容是整型变量a的地址,即00F8FB04。当我们将 aptr 作为函数参数传递时,只有 aptr 的内容(即地址)是函数参数的副本。所以,ptr 将收到 aptr 内容的副本(即地址 00F8FB04)
回答by Jim Buck
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.
如果您想潜在地更改指针本身,则可以使用指向指针的指针或对指针的引用。对于您最初的问题,从技术上讲,是的,所有参数都是按值传递的。
回答by nils
Yes it is, as it is in C.
是的,就像在 C 中一样。
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?
在这种情况下,使用指向指针的指针作为函数的参数来修改函数内的指针值是否可以接受/标准过程?
In which case? What do you want? You can use real references with the &
modifier.
在这种情况下?你想要什么?您可以使用带有&
修饰符的真实引用。
void func(type &ref);