Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

slippers2k

Call by reference is quite good, but what about return by reference?

This topic is 5557 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my continuing quest for C++ knowledge, I have found that it is possible to not just use call-by-reference to pass parameters to functions, but also to use a reference declaration for a return value: e.g. int &foo(int too, int zoo). Is this a relevant part of C++ code that I should look into, or have we effectively passed its profits in efficiency and what it does specifically? I am not going to use it for now until I understand it in more detail, but how important is it really? Thanks for your time. -slippers2k Attack life's problems like a Subaru... with all four wheels p.s. I should also say, is the benefit of using return by reference more useful than the risk of having dangling references? Thanks again. [edited by - slippers2k on April 1, 2003 11:18:36 PM]

Share this post


Link to post
Share on other sites
Advertisement
a function which returns a reference basicaly is the same as a function that returns a pointer


    
int gobalInteger = 9;

// returns a reference

int& foo()
{
return gobalInteger;
}

// returns a pointer

int *foobar()
{
return &gobalInteger;
}

int main(int argc, char **argv)
{
int *ptr = foobar();
int& ref = foo();

// change the value of ref like this:

ref = 6;

// its the same as doing this:

(*ptr) = 6;

return 0;
}

its just a reference cant be reassigned like a pointer
and (not sure about this) a reference dosnt have to be dereferenced (as much) like a pointer..

remember: scope matters when returning pointer-type values!

-- a dancing person appears insane to those who cannot hear the music
http://homepages.paradise.net.nz/danu/ -- Just click it

[edited by - silvermace on April 1, 2003 11:45:36 PM]

Share this post


Link to post
Share on other sites
quote:

...is the benefit of using return by reference more useful than the risk of having dangling references?



Yes, especially when the returning object contains dynamic data, like std::string. Returning by-value can cause a lot of allocations and copying, so by-reference is preferable, if possible. But, returning by-reference can be the source of a very hard to find bug and should be implemented with caution. Also, be sure to use const whenever possible.

There shouldn''t be any risk, if you know what you are doing. But as we''re all humans, I guess we don''t always do.

Scott Meyers deals with this topic in Effective C++, recommended!

Regards Mats

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!