简体   繁体   English

为什么或在什么情况下,您会将参数传递给函数作为C ++中的引用(或指针)?

[英]Why, or in what situations, would you pass an argument to a function as a reference (or pointer) in C++?

I am used to passing arguments regularly in my own code but frequently stumble upon function arguments being passed by reference or pointer while reading over others' C++ code. 我习惯于在自己的代码中定期传递参数,但是经常在阅读别人的C ++代码时偶然发现函数参数是通过引用或指针传递的。 I do not understand what is the purpose of doing so. 我不明白这样做的目的是什么。 Can somebody please explain this? 有人可以解释一下吗?

There are essentially three reasons why this is done. 这样做的原因基本上有三个。

Reduced memory usage 减少内存使用

Every time you call a function the arguments are copied and passed. 每次调用函数时,都会复制并传递参数。 This isn't a big deal when you are passing around numbers. 当您传递数字时,这没什么大不了的。 However when you are dealing with big chunks of memory, like objects, structs, arrays etc. this becomes very expensive. 但是,当您处理大块内存时,例如对象,结构,数组等,这将变得非常昂贵。

So all complex types are typically passed as pointers. 因此,所有复杂类型通常都作为指针传递。 If you are throwing around objects you are always working with a pointer. 如果要抛出对象,则始终使用指针。

The const qualifier should be used in this instance to indicate that the variable won't be changed. 在这种情况下,应使用const限定词表示该变量不会被更改。

Modify the argument 修改参数

Sometimes it is useful to modify the passed argument, though this should be avoided as bad style. 有时,修改传递的参数很有用,尽管应避免使用这种不良样式。 The best example I think is modifying an array, for example a push() function. 我认为最好的例子是修改数组,例如push()函数。 Another is modifying an objects public members, or when you want to return multiple values from a function. 另一个是修改对象的公共成员,或者要从函数返回多个值时。

Note that this can be a source of bugs. 请注意,这可能是错误的来源。 If you are modifying a passed variable it should be obvious from the name of the function that this is what you are doing. 如果要修改传递的变量,则从函数名称中可以明显看出这就是您正在执行的操作。

Low level memory access 低级内存访问

Anything which directly works with memory will want direct access to said memory. 任何直接与内存配合的东西都希望直接访问该内存。 Standard practice in C but less common in C++. C语言中的标准做法,但在C ++中不那么常见。 Look at functions like memcpy() and anything else from <string.h> . 查看类似memcpy()函数以及<string.h>其他任何函数。

If I pass a bunch of characters to a function, I can change them inside it and they will remain unchanged outside the function. 如果将一堆字符传递给函数,则可以在函数内部进行更改,它们在函数外部将保持不变。 This means if you wish to capitalise some text, you'd have to make a copy of it, then change some of the letters. 这意味着,如果您希望将某些文本大写,则必须对其进行复制,然后更改一些字母。

If on the other hand, you told the function the address of that string in memory, it could then change the existing string without making a copy. 另一方面,如果您告诉函数该字符串在内存中的地址,则它可以更改现有字符串而不进行复制。 Sometimes, the arguments required will consume a trivial size. 有时,所需的参数将消耗很小的大小。 Other-times, the data required make take several hundred mega/giga/tera bytes. 有时,所需的数据占用数百兆/千兆/兆字节。 The last thing you want to do is read all that in, then make a copy of it that you can send off to a function. 您要做的最后一件事是读入所有内容,然后制作一份副本以发送给函数。

The difference between references and pointers is mostly convenient syntax for the programmer, but there are important exceptions to this rule. 引用和指针之间的区别主要是对程序员来说方便的语法,但是该规则也有一些重要的例外。

If you pass argument to a function, you can change it inside the function. 如果将参数传递给函数,则可以在函数内部对其进行更改。 Once the execution of that function has finished, the passed in variable will rename unchanged. 该函数的执行完成后,传入的变量将重命名为不变。

int square_this_number(int x)
{
    int y = 0;
    y = x * x;

    x = 1000;
    return y;
}

int a = 10;
int b = 0;

b = square_this_number(a);
/* a is still 10, not 1000. */
/* b is 100. */

Pass by reference or pass by pointer means that you want to keep the change once the execution of function has finished. 通过引用传递或通过指针传递意味着您希望在函数执行完成后保留更改。

int try_square_and_change_input(int& x)
{
    int y = 0;
    y = x * x;

    x = 23;
    return y;
}

int a = 5;
int b = 0;

b = try_square_and_change_input(a);
/* a is now 23 instead of just 5. */
/* b is 25 of course. */

You may refer to this page: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr233.htm 您可以参考以下页面: https : //www.ibm.com/support/knowledgecenter/zh-CN/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr233.htm

Reasons to do this include: 这样做的原因包括:

  • I need to modify a variable that is not local to the called function. 我需要修改不是所调用函数本地的变量。
  • I want to pass a single word to the function, rather than make a copy of a large structure or array (and possibly another if I need to modify it). 我想将一个单词传递给函数,而不是复制大型结构或数组(如果需要修改,可能还要复制一个)。
  • It's a pointer to shared memory and I want changes to be visible in different processes or threads. 它是指向共享内存的指针,我希望更改在不同的进程或线程中可见。
  • It's a pointer to a polymorphic struct or union (or class in C++) and I want the function to be able to determine the correct type at runtime. 它是指向多态结构或联合(或C ++中的类)的指针,我希望该函数能够在运行时确定正确的类型。
  • The size and type of the data might vary, as with memcpy() . 数据的大小和类型可能会有所不同,例如memcpy()
  • The type should be opaque to client code. 该类型对于客户端代码应该是不透明的。
  • That's just how the interface is specified. 这就是指定接口的方式。

Pass by value: when we don't want to change variables value from called function. 按值传递:当我们不想更改被调用函数的变量值时。

Pass by reference: (only. In c++, not in c): when we want to do changes in variabe by called function. 通过引用传递:(仅在c ++中,而不在c中):当我们想通过调用函数对variabe进行更改时。

Pass by pointer: from periferi it works same as reference, but there exist differences.. 通过指针传递:与periferi相同,但与参考点相同。

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

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