Pointer vs. Reference

后端 未结 12 1407
闹比i
闹比i 2020-11-22 11:13

What would be better practice when giving a function the original variable to work with:

unsigned long x = 4;

void func1(unsigned long& val) {
     val          


        
相关标签:
12条回答
  • 2020-11-22 11:36

    Pass by const reference unless there is a reason you wish to change/keep the contents you are passing in.

    This will be the most efficient method in most cases.

    Make sure you use const on each parameter you do not wish to change, as this not only protects you from doing something stupid in the function, it gives a good indication to other users what the function does to the passed in values. This includes making a pointer const when you only want to change whats pointed to...

    0 讨论(0)
  • 2020-11-22 11:37

    I really think you will benefit from establishing the following function calling coding guidelines:

    1. As in all other places, always be const-correct.

      • Note: This means, among other things, that only out-values (see item 3) and values passed by value (see item 4) can lack the const specifier.
    2. Only pass a value by pointer if the value 0/NULL is a valid input in the current context.

      • Rationale 1: As a caller, you see that whatever you pass in must be in a usable state.

      • Rationale 2: As called, you know that whatever comes in is in a usable state. Hence, no NULL-check or error handling needs to be done for that value.

      • Rationale 3: Rationales 1 and 2 will be compiler enforced. Always catch errors at compile time if you can.

    3. If a function argument is an out-value, then pass it by reference.

      • Rationale: We don't want to break item 2...
    4. Choose "pass by value" over "pass by const reference" only if the value is a POD (Plain old Datastructure) or small enough (memory-wise) or in other ways cheap enough (time-wise) to copy.

      • Rationale: Avoid unnecessary copies.
      • Note: small enough and cheap enough are not absolute measurables.
    0 讨论(0)
  • 2020-11-22 11:37

    Pointers:

    • Can be assigned nullptr (or NULL).
    • At the call site, you must use & if your type is not a pointer itself, making explicitly you are modifying your object.
    • Pointers can be rebound.

    References:

    • Cannot be null.
    • Once bound, cannot change.
    • Callers don't need to explicitly use &. This is considered sometimes bad because you must go to the implementation of the function to see if your parameter is modified.
    0 讨论(0)
  • 2020-11-22 11:39

    Consider C#'s out keyword. The compiler requires the caller of a method to apply the out keyword to any out args, even though it knows already if they are. This is intended to enhance readability. Although with modern IDEs I'm inclined to think that this is a job for syntax (or semantic) highlighting.

    0 讨论(0)
  • 2020-11-22 11:43

    My rule of thumb is:

    Use pointers if you want to do pointer arithmetic with them (e.g. incrementing the pointer address to step through an array) or if you ever have to pass a NULL-pointer.

    Use references otherwise.

    0 讨论(0)
  • 2020-11-22 11:44

    A reference is similar to a pointer, except that you don’t need to use a prefix ∗ to access the value referred to by the reference. Also, a reference cannot be made to refer to a different object after its initialization.

    References are particularly useful for specifying function arguments.

    for more information see "A Tour of C++" by "Bjarne Stroustrup" (2014) Pages 11-12

    0 讨论(0)
提交回复
热议问题