C++ function parameters: use a reference or a pointer (and then dereference)?

后端 未结 11 2231
臣服心动
臣服心动 2021-02-05 10:49

I was given some code in which some of the parameters are pointers, and then the pointers are dereferenced to provide values. I was concerned that the pointer dereferencing wou

11条回答
  •  爱一瞬间的悲伤
    2021-02-05 11:03

    All the other answers already point out that neither function is superior to the other in terms of runtime performance.

    However, I think that that the former function is superior to the other in terms of readability, because a call like

    f( &a, &b );
    

    clearly expresses that a reference to some variable is passed (which, to me, rings the 'this object might be modified by the function' bell). The version which takes references instead of pointers just looks like

    f( a, b );
    

    It would be fairly surprising to me to see that a changed after the call, because I cannot tell from the invocation that the variable is passed by reference.

提交回复
热议问题