Passing a modifiable parameter to c++ function

前端 未结 12 1546
余生分开走
余生分开走 2020-12-06 19:12

Provided, I want to pass a modifiable parameter to a function, what should I choose: to pass it by pointer or to pass it by reference?

  1. bool GetFoo ( Foo& w
相关标签:
12条回答
  • 2020-12-06 19:18

    My preference is a reference. First, because it rhymes. :) Also because of the issues pointed out by other answers: no need to dereference, and no possibility of a reference being NULL. Another reason, which I have not seen mentioned, is that when you see a pointer you cannot be sure whether or not it points to dynamically allocated memory, and you may be tempted to call delete on it. A reference, on the other hand, dispenses with any ambiguity regarding memory management.

    Having said that, there are of course many cases when passing a pointer is preferable, or even necessary. If you know in advance that the parameter is optional, then allowing it to be NULL is very useful. Similarly, you may know in advance that the parameter is always dynamically allocated and have the memory management all worked out.

    0 讨论(0)
  • 2020-12-06 19:20

    Pass by reference, and avoid the whole NULL pointer problem.

    0 讨论(0)
  • 2020-12-06 19:23

    I prefer a reference instead of a pointer when:

    • It can't be null
    • It can't be changed (to point to something else)
    • It mustn't be deleted (by whoever receives the pointer)

    Some people say though that the difference between a reference and a const reference is too subtle for many people, and is invisible in the code which calls the method (i.e., if you read the calling code which passes a parameter by reference, you can't see whether it's a const or a non-const reference), and that therefore you should make it a pointer (to make it explicit in the calling code that you're giving away the address of your variable, and that therefore the value of your variable may be altered by the callee).

    I personally prefer a reference, for the following reason:

    1. I think that a routine should know what subroutine it's calling
    2. A subroutine shouldn't assume anything about what routine it's being called from.

    [1.] implies that making the mutability visible to the caller doesn't matter much, because the caller should already (by other means) understand what the subroutine does (including the fact that it will modify the parameter).

    [2.] implies that if it's a pointer then the subroutine should handle the possibility of the parameter's being a null pointer, which may be extra and IMO useless code.

    Furthermore, whenever I see a pointer I think, "who's going to delete this, and when?", so whenever/wherever ownership/lifetime/deletion isn't an issue I prefer to use a reference.

    For what it's worth I'm in the habit of writing const-correct code: so if I declare that a method has a non-const reference parameter, the fact that it's non-const is significant. If people weren't writing const-correct code then maybe it would be harder to tell whether a parameter will be modified in a subroutine, and the argument for another mechanism (e.g. a pointer instead of a reference) would be a bit stronger.

    0 讨论(0)
  • 2020-12-06 19:27

    The difference here is relatively minor.

    A reference cannot be NULL.

    A nullpointer may be passed. Thus you can check if that happens and react accordingly.

    I personally can't think of a real advantage of one of the two possibilities.

    0 讨论(0)
  • 2020-12-06 19:31

    These days I use const references for input parameters and pointers for out parameters. FWIIW, Google C++ Style Guide recommends the same approach (not that I always agree with their style guide - for instance they don't use exceptions, which usually does not make much sense)

    0 讨论(0)
  • 2020-12-06 19:32

    One advantage to passing by reference is that they cannot be null (unlike pointers), obviating the need to null-check every out parameter.

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