简体   繁体   中英

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. 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.

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. 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++. Look at functions like memcpy() and anything else from <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

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.
  • The size and type of the data might vary, as with 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.

Pass by pointer: from periferi it works same as reference, but there exist differences..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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