函数声明的返回类型中的&符号如何工作? [重复]

时间:2022-09-06 16:36:48

This question already has an answer here:

这个问题在这里已有答案:

In this piece of code, why f() is declared as "double & f(..."? What does it mean and how does it work? I don't even know what to google to find the answer to my question. Please help.

在这段代码中,为什么f()被声明为“double&f(...”?它是什么意思,它是如何工作的?我甚至不知道谷歌要找到我的问题的答案。请帮忙。

double a = 1, b = 2;
double & f (double & d) {
    d = 4;
    return b;
}

I know ampersand sign means the address of a variable or a function, but I don't see why it would make sense to write it when you are declaring a function.

我知道&符号表示变量或函数的地址,但我不明白为什么在声明函数时编写它是有意义的。

3 个解决方案

#1


13  

When the & operator is used in a declaration form, preceded by a type it doesn't mean "the address of" but a "reference to" which is essentially an automatically dereferenced pointer with disabled pointer arithmetic.

当&运算符在声明形式中使用时,前面有一个类型,它不是指“地址”而是“引用”,它本质上是一个带有禁用指针算法的自动解引用指针。

There are no references in C, so if you want to pass or return by reference, you had to pass a const pointer and dereference it to access the pointed to value. C++ added references to make this concept easier, to prevent accidental walking off the address with pointer arithmetic, and to save the need to dereference the pointer. This makes working with it much easier and resulting in cleaner and more readable syntax.

C中没有引用,因此如果要通过引用传递或返回,则必须传递一个const指针并取消引用它以访问指向的值。 C ++添加了引用,使这个概念更容易,防止用指针算术意外地离开地址,并节省了取消引用指针的需要。这使得使用它更容易,并使语法更清晰,更易读。

#2


21  

Consider these two functions: power2() and add():

考虑这两个函数:power2()和add():

void power2 (double& res, double x) {
    res = x * x;
}

double& add (double& x) {
    return ++x;
}

The first computes the power of x and stores the result in the first argument, res, – it does not need to return it.

第一个计算x的幂并将结果存储在第一个参数res中 - 它不需要返回它。

The second returns a reference, which means this reference can later be assigned a new value.

第二个返回一个引用,这意味着以后可以为该引用分配一个新值。

Example:

    double res = 0;

    power2(res, 5);
    printf("%f\n", res);


    printf("%f\n", ++add(res));

Output:

25.000000
27.000000

Please note that the second output is 27, not 26 – it's because of the use of ++ inside the printf() call.

请注意,第二个输出是27,而不是26 - 这是因为在printf()调用中使用了++。

#3


6  

In this case, the ampersand does not mean taking an address, but it denotes a reference. Here, f is a function that takes a reference to double as parameter and returns a reference to double.

在这种情况下,&符号并不意味着获取地址,但它表示引用。这里,f是一个函数,它引用double作为参数并返回对double的引用。

You might want to read about C++'s references in your textbook of choice, since they are a very basic part of the language.

您可能希望在您选择的教科书中阅读C ++的参考资料,因为它们是该语言的一个非常基本的部分。

#1


13  

When the & operator is used in a declaration form, preceded by a type it doesn't mean "the address of" but a "reference to" which is essentially an automatically dereferenced pointer with disabled pointer arithmetic.

当&运算符在声明形式中使用时,前面有一个类型,它不是指“地址”而是“引用”,它本质上是一个带有禁用指针算法的自动解引用指针。

There are no references in C, so if you want to pass or return by reference, you had to pass a const pointer and dereference it to access the pointed to value. C++ added references to make this concept easier, to prevent accidental walking off the address with pointer arithmetic, and to save the need to dereference the pointer. This makes working with it much easier and resulting in cleaner and more readable syntax.

C中没有引用,因此如果要通过引用传递或返回,则必须传递一个const指针并取消引用它以访问指向的值。 C ++添加了引用,使这个概念更容易,防止用指针算术意外地离开地址,并节省了取消引用指针的需要。这使得使用它更容易,并使语法更清晰,更易读。

#2


21  

Consider these two functions: power2() and add():

考虑这两个函数:power2()和add():

void power2 (double& res, double x) {
    res = x * x;
}

double& add (double& x) {
    return ++x;
}

The first computes the power of x and stores the result in the first argument, res, – it does not need to return it.

第一个计算x的幂并将结果存储在第一个参数res中 - 它不需要返回它。

The second returns a reference, which means this reference can later be assigned a new value.

第二个返回一个引用,这意味着以后可以为该引用分配一个新值。

Example:

    double res = 0;

    power2(res, 5);
    printf("%f\n", res);


    printf("%f\n", ++add(res));

Output:

25.000000
27.000000

Please note that the second output is 27, not 26 – it's because of the use of ++ inside the printf() call.

请注意,第二个输出是27,而不是26 - 这是因为在printf()调用中使用了++。

#3


6  

In this case, the ampersand does not mean taking an address, but it denotes a reference. Here, f is a function that takes a reference to double as parameter and returns a reference to double.

在这种情况下,&符号并不意味着获取地址,但它表示引用。这里,f是一个函数,它引用double作为参数并返回对double的引用。

You might want to read about C++'s references in your textbook of choice, since they are a very basic part of the language.

您可能希望在您选择的教科书中阅读C ++的参考资料,因为它们是该语言的一个非常基本的部分。