When to pass arguments to a function by reference and by address?

Can someone explain some examples when it is better to call functions by reference and when it is better to call at?

+3
source share
3 answers

Pass your arguments for use using the link whenever possible. Passing arguments by reference eliminates the possibility that they will be NULL. If you want to be able to pass a NULL value to a function, use a pointer.

+2
source

This has already been discussed. See Index versus Link .

+3
source

:

  • , ( ) .
  • Pass all other objects via the const link .

This makes it understandable for the caller with minimal documentation and zero cost performance, whose parameters are constant or not.

You can also apply this to primitive types, but it depends on whether you need to use const references for non-output parameters, since they are explicitly passed by value and cannot act as function output in any case (for direct types, not pointers / links - of course).

+2
source

Source: https://habr.com/ru/post/1697601/


All Articles