C++ functions: ampersand vs asterisk

前端 未结 9 1873
情书的邮戳
情书的邮戳 2020-12-13 03:43

Let\'s say you have a function that modifies a variable.

Should you write it like this: void myfunc(int *a) or like this void myfunc(int &a)

9条回答
  •  [愿得一人]
    2020-12-13 04:38

    I think I would disagre with @bb and @JaredPar and I lean to the opposite side of the fence. After years of trying to support other peoples C++ code, I often find problems lurking in non-obvious side effects of reference arguments. With C#, it is obvious since you have to prefix types of arguments with 'ref'/'out' but references are potentially confusing in C++. So, I like pointers because it's really clear something is coming back up. If you don't like points, C++ is not for you.

提交回复
热议问题