Where ampersand “&” can be put when passing argument by reference?

若如初见. 提交于 2019-11-30 16:42:52

问题


In the examples that I saw the arguments were passed by reference in the following way:

void AddOne(int &y)

In the code that I have I see the following syntax:

void AddOne(int& y)

I wonder if it is the same or the second case is somehow different from the first one.


回答1:


Both are exactly the same. No difference at all.

All that matters is that & should be between the type and the variable name. Spaces don't matter.

So

void AddOne(int&  y);
void AddOne(int  &y);
void AddOne(int & y)
void AddOne(int   &     y);
void AddOne(int&y);

are same!




回答2:


It's the same for the language, just different code conventions




回答3:


There is no differences between

void AddOne(int &y);

and

void AddOne(int& y);

and even

void AddOne(int&y);

in C++, as the whitespaces between actual tokens are discarded.



来源:https://stackoverflow.com/questions/15390345/where-ampersand-can-be-put-when-passing-argument-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!