What#39;s the semantically accurate position for the ampersand in C++ references(C++ 引用中 符号在语义上的准确位置是什么)
问题描述
众所周知,声明指针的语义准确方法是
It's pretty common knowledge that the semantically accurate way to declare pointers is
int *x;
代替
int* x;
这是因为 C 将 *x
视为 int,而不是将 x
视为 int 指针.
This is because C sees *x
as an int, not x
as an int pointer.
这很容易证明
int* a, b;
其中a
是一个int 指针,而b
是一个int.
where a
is an int pointer, while b
is an int.
在 Stack Overflow 上至少有 5 个重复的问题讨论了这个问题的指针.但是引用呢?
There are at least 5 duplicate questions on Stack Overflow that discuss this issue for pointers. But what about references?
推荐答案
在研究这个问题时,我已经找到了答案:
While researching for this question, I already found the answer:
&
需要像 *
一样编写.
The &
needs to be written just like the *
.
演示代码类似于指针演示代码:
The demonstration code is similar to the pointer demonstration code:
int main() {
int a = 0;
int b = 1;
int& ar = a, br = b;
br = 2;
return b;
}
这返回 1,这意味着 ar
是一个 int 引用,而 br
只是一个整数.
This returns 1, which means that ar
is an int reference, while br
is just an integer.
这篇关于C++ 引用中 & 符号在语义上的准确位置是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 引用中 & 符号在语义上的准确位置是什么


基础教程推荐
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09