简体   繁体   中英

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. 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. Anyways i understood the example but the statement that confused me is 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. 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). 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& . 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) . 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:

sysop s;
use(s);

use works on s directly, not its copy (this is how references work). That is required, because use modifies its parameter:

sysopref.used++;

However, it returns its parameter as const reference (probably to avoid further modifications or something). It doesn't matter, that parameter is non-const reference - it is "promoted" (but it wouldn't work the other way around).

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). 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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