问题描述
众所周知,声明指针的语义准确方式是
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.
stackoverflow.com上至少有5个重复的问题,这些问题针对指针进行了讨论.但是引用呢?
There are at least 5 duplicate questions on stackoverflow.com that discuss this issue for pointers. But what about references?
推荐答案
在研究此问题时,我已经找到了答案:
While researching for this question, I already found the answer:
&
必须像 *
一样编写.
该演示代码类似于指针演示代码:
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 ++参考中的&符在语义上的准确位置是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!