简体   繁体   English

是否传递指针参数,通过 C++ 中的值传递?

[英]Is passing pointer argument, pass by value in C++?

Is passing pointer argument, pass by value in C++?是否传递指针参数,通过 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?在这种情况下,使用指向指针的指针作为 function 的参数来修改 function 中的指针值是否可以接受/标准过程?

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.这是标准的 C 语言做事方式。

When using C++, using references is preferred to pointer (henceforth also to pointer to pointer).使用 C++ 时,使用引用优于指针(此后也使用指向指针的指针)。

For the why references 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.它们打破了 C 语言的简单按值传递规则,是什么让理解函数关于参数的行为(它们会被改变吗?)不太明显。 You also need function prototype to be sure.您还需要函数原型来确定。 But that is not really worse than the multiple pointer levels necessary when using C.但这并不比使用 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++ 程序时,这可能是一个问题(但这不是最常见的情况)。

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.在指针到指针的特定情况下,区别主要是简单,但使用引用也可能很容易删除两级指针并只传递一个引用而不是指向指针的指针。

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) //copy some value to local variable x (of type int)
{
   x = 5; //modify local variable. lost after function call
}

void func2(int *x) //copy some value to local variable x (of type int*)
{
   int a;
   x = &a; //modify local variable. lost after function call.
}

void func3(int *x) //copy some value to local variable x(of type int*)
{
   *x = 10; //x is local but *x is not! change is saved after function call!
}

func1 and func2 are identical. func1 和 func2 是相同的。 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). func3 有能力改变另一个内存位置(一个不是函数本地的变量)。

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.但是在指针类型的情况下,我们有一种方法可以更改内存中远程地址的内容。

Pass by value using Pointers I'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).这里,在主函数中,a 是一个整数变量,其内容为 10,地址为 00F8FB04(假设)。 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. aptr是整型指针,存放整型变量a的地址,所以aptr内容是整型变量a的地址,即00F8FB04。 When we pass aptr as the function argument only content of aptr (that is address) are copies to function parameter.当我们将 aptr 作为函数参数传递时,只有 aptr 的内容(即地址)是函数参数的副本。 So, ptr will receive the copy of content of aptr (that is address 00F8FB04)所以,ptr 将收到 aptr 内容的副本(即地址 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.对于您最初的问题,从技术上讲,是的,所有参数都是按值传递的。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM