在 C++ 中从函数返回中引用运算符 & 的使用

Use of reference operator & in returning from function in c++

我对这里的代码感到困惑:

#include <iostream>
#include <ctime>
using namespace std;
double vals[] = { 10.1, 12.6, 33.1, 24.1, 50.0 };

double& setValues(int i)//if i remove the &operator, i get an error, why ?
{
    return vals[i]; // return a reference to the ith element
}

int main()
{
    cout << "Value before change" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "vals[" << i << "] = ";
        cout << vals[i] << endl;
    }
    setValues(1) = 20.23; // change 2nd element
    setValues(3) = 70.8; // change 4th element
    cout << "Value after change" << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << "vals[" << i << "] = ";
        cout << vals[i] << endl;
    }
    return 0;
}

如果我删除函数中的 & 运算符

double& setValues(int i);

我得到的错误是 'l-value required as the left operand of the assignment' 这是否意味着值 vals[i] 应该放在右边?使用 & 运算符,这部分变为左值 ?

Is that means the value vals[i] should be placed in the right?

没有。 vals[i]既可以是赋值运算符的左操作数,也可以是右操作数。它是函数返回的 double value,不能是运算符的左操作数。

with & operator this part becomes l-value ?

这个&不是运算符而是声明符的一部分。使用 &,函数返回一个 reference,您可以将其用作运算符的左操作数。

当您创建变量或数组并为其命名时,它们会预留一块内存区域供它们定位。

double vals[] = { 10.1, 12.6, 33.1, 24.1, 50.0 };

该数组的每个元素都有一个不同的内存地址。

如果没有 & 引用声明符,您的函数将简单地 return 数组中值的 副本

double setValues(int i); // return a copy of a value from the array

这个副本的问题是它没有在内存中被赋予一个永久的位置。这就是我们所说的 temporary 值,它的寿命与创建它的表达式一样长。一旦表达式结束,临时值就会被销毁。

所以:

double d = setValue(3); // okay

尽管函数名称不是很合适,但编译器允许这样做,因为数组中值的临时副本存储在称为[=17的永久(ish)位置=] 在 临时 被销毁之前。

因此,允许创建 临时对象 的表达式存在于赋值的右侧 是很有用的,它们的值可以是被作业左侧捕获

但反之则不然:

setValue(3) = 70.8; // not allowed

原因是 setValue(3) return 是数组中值的 临时副本 ,将在赋值后立即销毁。因此,允许写入这样一个 临时值 是没有意义的,如果它的值将在之后立即被删除。

此外,如果编译器允许对这样的临时对象进行赋值,则必须为其预留实际内存(而不仅仅是一个 CPU 寄存器)并且编译器会在很多地方将失去更有效地处理临时事件的机会。

当您使用 reference 声明符将函数更改为 return a reference 时,所有更改:

double& setValues(int i); // return access to the value in the array

returns 是访问数组本身中的实际值。这不是临时值,而是驻留在主内存中的永久(大概)值。

所以现在编译器允许您更改它的值是有意义的,因为它不会在表达式结束后被销毁,而是会继续存在直到数组本身被销毁。

setValue(3) = 70.8; // now it is good - changes the array itself