简体   繁体   English

关于C ++的困惑从函数返回引用

[英]Confusion on C++ return reference from function

I have been having some confusion on the reference in C++, so I did a little experiment with the following function 我一直对C ++中的引用感到困惑,所以我做了一个小实验,使用了以下函数

std::vector<float> & fun(std::vector<float> & x) {
    return x;
}

Then I call the function using the following two ways: 然后我使用以下两种方式调用该函数:

std::vector<float> x(10000);
std::vector<float> result1 = fun(x);
std::vector<float> & result2 = fun(x);

Now the variable result2 is indeed a reference of x, but result1 seems to be a copy of x instead of a reference. 现在变量result2确实是x的引用,但result1似乎是x的副本而不是引用。 So here I am confused: 所以我在这里感到困惑:

Why can I declare a non-reference variable to be the return value of a function that returns a reference? 为什么我可以将非引用变量声明为返回引用的函数的返回值?

Does C++ changes my returned reference to a non-reference variable because my declaration of result1 is not a reference? C ++是否更改了我对非引用变量的返回引用,因为我的result1声明不是引用?

Since fun(x) simply accepts a reference to x and returns it, your code (on the left) is equivalent to the code on the right: 因为fun(x)只接受对x的引用并返回它,所以你的代码(在左边)等同于右边的代码:

a std::vector<float> x(10000);          a std::vector<float> x(10000);

std::vector<float> result1 = fun(x);    std::vector<float> result1 = x;
std::vector<float> &result2 = fun(x);   std::vector<float> &result2 = x;

In other words, result1 becomes a copy of x , while result2 becomes a reference to x . 换句话说, result1成为x副本 ,而result2成为对x引用 That means result1 is distinct from x , while result2 and x refer to the same underlying data. 这意味着result1x不同,而result2x指的是相同的底层数据。

It's really no different to: 它真的没有什么不同:

int a = 42;
int b = a;
int &c = a;

At that point, b is an independent copy that, if changed, will not affect the other two variables. 此时, b是一个独立的副本,如果更改,将不会影响其他两个变量。 However, since a and c refer to the same underlying data, changing one will affect the other. 但是,由于ac引用相同的基础数据,因此更改一个将影响另一个。

That's basically the same as 这与...基本相同

std::vector<float> x(10000);
std::vector<float> & result2 = x;
std::vector<float> result1 = result2;

The language allows it since result2 is a reference of x , each mention of result2 is the same as x . 语言允许它,因为result2x的引用,每次提到result2都与x相同。 You could see the reference as being an alias , result2 is an alias of x . 您可以将引用视为别名result2x别名

There's no real change happening. 没有真正的变化发生。

What happens is that a reference refers to something, and when you use the reference in an expression, you (usually) get what it refers to. 会发生什么是引用是指某种东西,当你在表达式中使用引用时,你(通常)得到它所指的东西。 So, if I have something like: 所以,如果我有类似的东西:

int x = 1;
int &rx = x;

std::cout << rx;

What gets printed out is the value of x --the reference gets dereferenced to get the value, and that's what gets printed out. 打印出来的是x的值 - 引用被取消引用以获取值,这就是打印出来的内容。 Likewise, if I had something like: 同样,如果我有类似的东西:

int x = 1;
int y = 2;
int rx = x;
int ry = y;
int z = rx + ry;

...the compiler is going to dereference rx and ry to get the values of x and y . ...编译器将取消引用rxry以获取xy的值。 Then it adds those together, and assigns the result to z . 然后将它们加在一起,并将结果赋给z

Summary 摘要

A reference is an alias for some object. 引用是某个对象的别名。 Under most circumstances (essentially anything other than initializing another reference) using the reference in an expression uses the object that the reference refers to, not the reference itself. 在大多数情况下(基本上除初始化另一个引用之外的任何东西)使用表达式中的引用使用引用所引用的对象,而不是引用本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM