   Call by reference is quite good, but what about return by reference?    
  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 wheelsp.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]
  a function which returns a reference basicaly is the same as a function that returns a pointerint gobalInteger = 9;// returns a referenceint& foo(){   return gobalInteger;}// returns a pointerint *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 pointerand (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];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  