[英]& in function declaration return type
It means return value by reference : 这意味着通过引用返回值 :
int& GetValue()
^ Means returns a reference of int
Like 喜欢
int i = 10;
int& GetValue() {
int &j = i;
return j;
}
j
is a reference of i
, a global variable. j
是全局变量i
的引用。
Note: In C++ you have three kinds of variables: 注意:在C ++中,您具有三种变量:
int i = 10
. int i = 10
。 int &j = i;
int &j = i;
reference variable creates alias of other variable, both are symbolic names of same memory location. int* ptr = &i
. int* ptr = &i
。 called pointers. Deference in declaration 尊重申报
Pointer:
int *ptr = &i;
^ ^ & is on the left side as an address operation
|
* For pointer variable.
Reference:
int &j = i;
^
| & on the right side for reference
Remember in C you have only two kinds of variables value and address (pointer). 请记住,在C语言中,只有值和地址(指针)这两种变量。 Reference variables are in C++, and references are simple to use like value variables and as capable as pointer variables.
引用变量使用C ++,并且引用很容易使用,例如值变量和指针变量。
Pointers are like: 指针就像:
j = &i;
i j
+------+ +------+
| 10 | | 200 |
+------+ +------+
202 432
Reference are like: 参考如下:
int &j = i;
i, j
+------+
| 10 |
+------+
No memory is allocated for j. 没有为j分配内存。 It's an alias of the same location in memory.
它是内存中相同位置的别名。
What are the differences between pointer variable and reference variable in C++? C ++中的指针变量和引用变量之间有什么区别?
Because as you commented, you have questions about the differences between pointers and reference variables, so I highly encourage you read the related pages from the link I have given in my answer. 因为正如您所评论的那样,您对指针和引用变量之间的区别有疑问,所以我强烈建议您从我在答案中给出的链接中阅读相关页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.