简体   繁体   English

&in函数声明返回类型

[英]& in function declaration return type

What does the & mean in the following? &在下面表示什么?

class Something
{
 public:
   int m_nValue;

   const int& GetValue() const { return m_nValue; }
   int& GetValue() { return m_nValue; }
};

This code is taken from here . 此代码是从这里获取的

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 ++中,您具有三种变量:

  1. Value variable for example int i = 10 . 值变量,例如int i = 10
  2. Reference variable for example int &j = i; 参考变量例如int &j = i; reference variable creates alias of other variable, both are symbolic names of same memory location. 引用变量创建其他变量的别名,它们都是相同存储位置的符号名称。
  3. Address variable: int* ptr = &i . 地址变量: int* ptr = &i called pointers. 称为指针。 Pointer variables use for holding address of a variable. 指针变量用于保存变量的地址。

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 ++中的指针变量和引用变量之间有什么区别?

  1. A pointer can be re-assigned any number of times while a reference can not be reassigned after initialization. 指针可以被重新分配任意次,而初始化后不能重新分配引用。
  2. A pointer can point to NULL while a reference can never point to NULL 指针可以指向NULL,而引用不能指向NULL
  3. You can't take the address of a reference like you can with pointers 您不能像使用指针一样获取引用的地址
  4. There's no “reference arithmetics” (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in &obj + 5). 没有“引用算术”(但是您可以使用引用指向的对象的地址,并像&obj + 5中那样对它执行指针算术)。

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.

相关问题 函数声明的返回类型中的const关键字 - const keyword in return type of function declaration 已具有返回类型的函数声明中的模板声明? - Template declaration in function declaration that already has a return type? 如何将模板 function 的声明和定义与尾随返回类型分开? - How to separate declaration and definition for template function with trailing return type? 为什么不能将auto用作函数声明的返回类型 - Why can auto not be used as a return type for function declaration 是否可以在函数指针声明中使用`auto`关键字作为返回类型进行初始化? - Is it possible to use the `auto` keyword as a return type in a function pointer declaration with initialization? 前向声明方法返回类型 - Forward declaration of a method return type 是否可以在C ++中使用函数返回类型作为参数来声明另一个函数? - Is it possible to use function return type as an argument in declaration of another function in C++? 返回函数类型的前向声明 - Forward declaration of returned type of function 为什么有必要在函数定义中包含返回类型,即使它在声明中指定了? - Why is it necessary to include the return type in a function definition even if it's specified in the declaration? 函数声明和定义的返回类型不匹配,虽然编译器还可以吗? - Return type of function declaration & definition didn't match, compiler was ok with it though?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM