简体   繁体   English

从函数返回对结构的引用

[英]Returning a reference to a structure from a function

Hi I was reading about reference variables in c++ and was reading about reference to a structure and returning a reference to a structure from a function. 嗨,我正在阅读有关c ++中的引用变量的信息,并正在阅读有关对结构的引用,并从函数返回对结构的引用。 The example code i read is below : 我读的示例代码如下:

#include <iostream>
using namespace std;

struct sysop {
  char name[26];
  char quote[64];
  int used;
};

const sysop & use(sysop & sysopref);

int main()
{
  sysop looper = {"Rick Looper", "I'm a goto kind of guy.", 0};
  use (looper);
  cout << “Looper: “ << looper.used << “ use(s)\n”;
  sysop copycat;
  copycat = use(looper);
  cout << “Looper: “ << looper.used << “ use(s)\n”;
  cout << “Copycat: “ << copycat.used << “ use(s)\n”;
  cout << “use(looper): “ << use(looper).used << “ use(s)\n”;
  return 0;
}

const sysop & use(sysop & sysopref)
{
  cout << sysopref.name << “ says:\n”;
  cout << sysopref.quote << endl;
  sysopref.used++;
  return sysopref;
}

This is from C++ Primer Plus book. 这是来自C ++ Primer Plus的书。 Anyways i understood the example but the statement that confused me is use(looper) . 无论如何,我理解了示例,但令我感到困惑的是use(looper) I mean the function prototype says to return a reference to a constant structure variable, but in this statement, the function is not returning any reference. 我的意思是函数原型说要返回对常量结构变量的引用,但是在此语句中,函数不返回任何引用。 I am not sure if there is special thing about reference to constant structures or something that allows the function to be used without returning the data or what. 我不确定是否有关于常量结构的引用或允许函数在不返回数据的情况下使用的特殊内容。

Can someone please explain me this?? 有人可以向我解释一下吗?

You said: 你说:

but in this statement, the function is not returning any reference. 但是在此语句中,该函数未返回任何引用。

That is incorrect. 那是不对的。 The function still returns a const reference. 该函数仍返回const引用。 The return value is being ignored at the point of the call. 返回值在调用时被忽略。

You could use: 您可以使用:

sysop const& ref = use (looper);

to capture the return value of the function. 捕获函数的返回值。

the statement that confused me is use(looper). 令我感到困惑的是use(looper)。 I mean the function prototype says to return a reference to a constant structure variable, but in this statement, the function is not returning any reference. 我的意思是函数原型说要返回对常量结构变量的引用,但是在此语句中,函数不返回任何引用。

Yes, it does. 是的,它确实。 You have to read the whole statement, particularly where it says the return type is sysop const& . 您必须阅读整个语句,尤其是在声明返回类型为sysop const& That's a reference type. 这是参考类型。

In this case you pass a reference into the function and pass the same reference straight back out. 在这种情况下,您可以将引用传递给函数,然后直接返回相同的引用。

I mean the function prototype says to return a reference to a constant structure variable, but in this statement, the function is not returning any reference. 我的意思是函数原型说要返回对常量结构变量的引用,但是在此语句中,函数不返回任何引用。

sysopref is already a reference when you accept it as a parameter in use use(sysop & sysopref) . 当您接受sysopref作为使用中的参数use(sysop & sysopref)时,它已经是参考。 You simply chanel it when you return the variable from your function. 当您从函数返回变量时,只需对它进行香奈儿。

const sysop & use(sysop & sysopref)

use accepts non-const reference to instance of type sysop - that means, that when we call use this way: use接受对sysop类型实例的非常量引用-这意味着,当我们调用时,请use这种方式:

sysop s;
use(s);

use works on s directly, not its copy (this is how references work). use直接在s工作,而不是在它的副本上工作(这是引用的工作方式)。 That is required, because use modifies its parameter: 这是必需的,因为use修改了其参数:

sysopref.used++;

However, it returns its parameter as const reference (probably to avoid further modifications or something). 但是,它返回其参数作为const引用(可能避免进一步的修改或其他操作)。 It doesn't matter, that parameter is non-const reference - it is "promoted" (but it wouldn't work the other way around). 没关系,该参数是非const引用-它是“提升”的(但反之亦然)。

So, after this sequence of calls: 因此,在此调用序列之后:

sysop s;
const sysop& ret_ref = use(s);

ret_ref will be a reference to s (they will share the memory and contain exactly the same data). ret_ref将是对s的引用(它们将共享内存并包含完全相同的数据)。 Look at this sample: 看这个例子:

int main()
{
    sysop s;
    s.used = 0;

    const sysop& ret_ref = use(s);

    std::cout<<s.used<<std::endl;
    std::cout<<ret_ref.used<<std::endl<<std::endl;

    std::cout<<&(s)<<std::endl;
    std::cout<<&(ret_ref)<<std::endl;

    return 0;
}

Output: 输出:

1
1

0xbfc910f0
0xbfc910f0

As you can see, their memory addresses are exactly the same, so function indeed returned passed parameter. 如您所见,它们的内存地址完全相同,因此函数确实返回了传递的参数。

Try this code here . 在此处尝试此代码。

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

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