Passing a modifiable parameter to c++ function

前端 未结 12 1550
余生分开走
余生分开走 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:36

    I'd recommend that you consider (may not be best for every situation) returning Foo from the function rather than modifying a parameter. Your function prototype would look like this:

    Foo GetFoo() // const (if a member function)
    

    As you appear to be returning a success/failure flag, using an exception might be a better strategy.

    Advantages:

    • You avoid all of the pointer/reference issues
    • Simplifies life for the caller. Can pass the return value to other functions without using a local variable, for example.
    • Caller cannot ignore error status if you throw an exception.
    • Return value optimization means that it may be as efficient as modifying a parameter.
    0 讨论(0)
  • 2020-12-06 19:38

    Advantages to passing by reference:

    • Forces user to supply a value.
    • Less error-prone: Handles pointer dereferencing itself. Don't have to check for null inside.
    • Makes the calling code look much cleaner.

    Advantages to passing pointer by value:

    • Allows null to be passed for "optional" parameters. Kinda an ugly hack, but sometimes useful.
    • Forces caller to know what is being done w/ the parameter.
    • Gives the reader half a clue of what might be being done w/ the parameter without having to read the API.

    Since reference passing is in the language, any non-pointer parameters might be getting modified too, and you don't know that pointer values are being changed. I've seen APIs where they are treated as constants. So pointer passing doesn't really give readers any info that they can count on. For some people that might be good enough, but for me it isn't.

    Really, pointer passing is just an error-prone messy hack leftover from C which had no other way to pass values by reference. C++ has a way, so the hack is no longer needed.

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

    I choose #2 because it obvious at the point of call that the parameter will be changed.

    GetFoo(&var) rather than GetFoo(var)

    I prefer pass by reference for just const references, where I am trying to avoid a copy constructor call.

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

    I seem to recall that in c++ references where not null and pointers could be. Now I've not done c++ for a long time so my memory could be rusty.

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

    I find this a matter of personal taste. I actually prefer to pass by reference because pointers give more freedom but they also tend to cause a lot of problems.

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

    The benefit to a pointer is that you can pass nothing, ie. use it as if the parameter was completely optional and not have a variable the caller passes in.

    References otherwise are safer, if you have one its guaranteed to exist and be writeable (unless const of course)

    I think its a matter of preference otherwise, but I don't like mixing the two as I think it makes maintainace and readability of your code harder to do (especially as your 2 functions look the same to the caller)

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