When to use references vs. pointers

前端 未结 17 2113
一向
一向 2020-11-22 02:27

I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an

17条回答
  •  温柔的废话
    2020-11-22 03:08

    Any performance difference would be so small that it wouldn't justify using the approach that's less clear.

    First, one case that wasn't mentioned where references are generally superior is const references. For non-simple types, passing a const reference avoids creating a temporary and doesn't cause the confusion you're concerned about (because the value isn't modified). Here, forcing a person to pass a pointer causes the very confusion you're worried about, as seeing the address taken and passed to a function might make you think the value changed.

    In any event, I basically agree with you. I don't like functions taking references to modify their value when it's not very obvious that this is what the function is doing. I too prefer to use pointers in that case.

    When you need to return a value in a complex type, I tend to prefer references. For example:

    bool GetFooArray(array &foo); // my preference
    bool GetFooArray(array *foo); // alternative
    

    Here, the function name makes it clear that you're getting information back in an array. So there's no confusion.

    The main advantages of references are that they always contain a valid value, are cleaner than pointers, and support polymorphism without needing any extra syntax. If none of these advantages apply, there is no reason to prefer a reference over a pointer.

提交回复
热议问题