通过引用传递参数时,与号 "&" 可以放在哪里?
Where ampersand "&" can be put when passing argument by reference?
在我看到的示例中,参数以以下方式通过引用传递:
void AddOne(int &y)
在我所拥有的代码中,我看到了以下语法:
void AddOne(int& y)
我想知道它是相同的还是第二个案例与第一个不同。
两者完全相同。没有差别。
重要的是&
应该位于类型和变量名称之间。空间不重要。void AddOne(int& y);
void AddOne(int &y);
void AddOne(int & y)
void AddOne(int & y);
void AddOne(int&y);
是相同的!
对于语言来说是一样的,只是代码约定不同
void AddOne(int &y);
和
void AddOne(int& y);
和
void AddOne(int&y);
,因为实际标记之间的空白被丢弃。
这是一个风格问题。都是正确有效的
然而,Bjarne Stroustrup似乎更喜欢将&
和*
放在类型名称旁边,强调变量的类型:
int* i; // i is a pointer to an int
int& j; // j is a reference to an int
参见此处获取指针:https://www.stroustrup.com/bs_faq2.html#whitespace
参见"使用c++编程原则和实践"中的示例;书和参考资料在这里:https://stackoverflow.blog/2019/10/11/c-creator-bjarne-stroustrup-answers-our-top-five-c-questions/
相关文章:
- C++我需要了解在哪里使用指针和双指针
- 未定义的引用在哪里
- 关于在此上下文中使用与号(&)的说明
- 当我删除数组时,指针在哪里?指向数组的指针与数组中的元素之间的区别
- 与前缀相关的整数解析在哪里定义?
- 用于在C++中返回引用的语法-与号
- 在哪里可以找到与Clang假设的目录结构相同的mingw32
- 三重与号"&&&"在C++中代表什么?
- 与号和与常量在构造函数中
- 在哪里可以找到与Microsoft DLL(比如WTTLog.DLL)相对应的头文件和库文件
- 如果我知道数据在哪里,我能知道什么关于数据,例如0xffffffff7fffd9d8与0x10019c1e0
- 与号 (&) 在 C++ 中如何工作?
- 自动对象驻留在哪里(与演示一起)
- 通过引用传递参数时,与号 "&" 可以放在哪里?
- 与qt设计器中所做的更改相对应的代码在哪里?
- 当路径不包括路径或驱动器号时,文件放置在哪里
- 在哪里放置要与Windows用户的桌面设置一起漫游的用户数据文件?
- 与号 (&) 的含义,在 .rc 文件中
- (&) - 括号中的与号 - 在此代码中是什么意思?
- 在C++中,指针和带有常量的与号位置