Passing a modifiable parameter to c++ function

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

提交回复
热议问题