在 C++ 中,函数返回类型后的 & 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2379859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:15:12  来源:igfitidea点击:

In C++, what does & mean after a function's return type?

c++

提问by NellerLess

In a C++ function like this:

在这样的 C++ 函数中:

int& getNumber();

what does the &mean? Is it different from:

是什么&意思?是否不同于:

int getNumber();

采纳答案by Nick Meyer

Yes, the int&version returns a reference to an int. The intversion returns an intby value.

是的,该int&版本返回对int. 该int版本将返回一个int由值。

See the section on referencesin the C++ FAQ

请参阅C++ FAQ 中有关参考的部分

回答by sergiom

It's different.

这是不同的。

int g_test = 0;

int& getNumberReference()
{
     return g_test;
}

int getNumberValue()
{
     return g_test;
}

int main()
{
    int& n = getNumberReference();
    int m = getNumberValue();
    n = 10;
    cout << g_test << endl; // prints 10
    g_test = 0;
    m = 10;
    cout << g_test << endl; // prints 0
    return 0;
}

the getNumberReference() returns a reference, under the hood it's like a pointer that points to an integer variable. Any change applyed to the reference applies to the returned variable.

getNumberReference() 返回一个引用,在幕后它就像一个指向整数变量的指针。应用于引用的任何更改都适用于返回的变量。

The getNumberReference() is also a left-value, therefore it can be used like this:

getNumberReference() 也是一个左值,因此它可以像这样使用:

getNumberReference() = 10;

回答by Tristram Gr?bener

Yes, it's different.

是的,它是不同的。

The & means you return a reference. Otherwise it will return a copy (well, sometimes the compiler optimizes it, but that's not the problem here).

& 表示您返回一个引用。否则它会返回一个副本(好吧,有时编译器会优化它,但这不是这里的问题)。

An example is vector. The operator[] returns an &. This allows us to do:

一个例子是向量。operator[] 返回一个 &。这使我们能够做到:

my_vector[2] = 42;

That wouldn't work with a copy.

这不适用于副本。

回答by T.E.D.

The difference is that without the &what you get back is a copy of the returned int, suitable for passing into other routines, comparing to stuff, or copying into your own variable.

不同之处在于,如果没有&返回的是返回的 int 的副本,则适合传递到其他例程中,进行比较或复制到您自己的变量中。

With the &, what you get back is essentially the variable containingthe returned integer. That means you can actually put it on the left-hand side of an assignment, like so:

使用&,您得到的本质上是包含返回整数的变量。这意味着您实际上可以将它放在 assignment 的左侧,如下所示:

getNumber() = 200;

回答by codaddict

int& getNumber(): function returns an integer by reference.

int& getNumber(): 函数通过引用返回一个整数。

int getNumber(): function returns an integer by value.

int getNumber(): 函数按值返回一个整数。

They differ in some ways and one of the interesting differences being that the 1st type can be used on the left side of assignment which is not possible with the 2nd type.

它们在某些方面有所不同,其中一个有趣的区别是第一种类型可以在赋值的左侧使用,而第二种类型则不可能。

Example:

例子:

int global = 1;

int& getNumber() {
        return global; // return global by reference.
}

int main() {

        cout<<"before "<<global<<endl;
        getNumber() = 2; // assign 2 to the return value which is reference.
        cout<<"after "<<global<<endl;

        return 0;
}

Ouptput:

输出:

before 1
after 2

回答by fredoverflow

The first version allows you to write getNumber() = 42, which is probably not what you want. Returning references is very useful when overloading operator[]for your own containers types. It enables you to write container[9] = 42.

第一个版本允许您编写getNumber() = 42,这可能不是您想要的。在operator[]为您自己的容器类型重载时,返回引用非常有用。它使您能够编写container[9] = 42.

回答by jldupont

"&" means reference, in this case "reference to an int".

“&” 表示引用,在这种情况下是“对 int 的引用”。

回答by Shayan

It means that it is a reference type. What's a reference?

这意味着它是一个引用类型。什么是参考?

Wikipedia:

维基百科:

In the C++ programming language, a reference is a simple reference datatype that is less powerful but safer than the pointer type inherited from C. The name C++ reference may cause confusion, as in computer science a reference is a general concept datatype, with pointers and C++ references being specific reference datatype implementations. The declaration of the form:

Type & Name

where is a type and is an identifier whose type is reference to .

Examples:

  1. int A = 5;
  2. int& rA = A;
  3. extern int& rB;
  4. int& foo ();
  5. void bar (int& rP);
  6. class MyClass { int& m_b; /* ... */ };
  7. int funcX() { return 42 ; }; int (&xFunc)() = funcX;

Here, rA and rB are of type "reference to int", foo() is a function that returns a reference to int, bar() is a function with a reference parameter, which is reference to int, MyClass is a class with a member which is reference to int, funcX() is a function that returns an int, xFunc() is an alias for funcX.

在 C++ 编程语言中,引用是一种简单的引用数据类型,它不如从 C 继承的指针类型强大但更安全。 C++ 引用的名称可能会引起混淆,因为在计算机科学中,引用是一个通用概念数据类型,具有指针和C++ 引用是特定的引用数据类型实现。声明形式:

类型和名称

where 是一个类型, 是一个标识符,其类型是对 的引用。

例子:

  1. 整数 A = 5;
  2. int&rA = A;
  3. 外部内部& rB;
  4. int&foo();
  5. 空栏 (int&rP);
  6. class MyClass { int& m_b; /* ... */ };
  7. int funcX() { 返回 42 ; }; int (&xFunc)() = funcX;

这里,rA 和 rB 是“对 int 的引用”类型,foo() 是一个返回对 int 的引用的函数,bar() 是一个带有引用参数的函数,它是对 int 的引用,MyClass 是一个带有成员引用 int,funcX() 是一个返回 int 的函数,xFunc() 是 funcX 的别名。

Rest of the explanation is here

其余的解释在这里

回答by CppLearner

It's a reference

这是一个参考

回答by Simon

It means it's returning a referenceto an int, not an int itself.

这意味着它返回对 int的引用,而不是 int 本身。