11

I feel like this question is basic enough to be out there somewhere, but I can't seem to be able to find an answer for it.

Suppose I have this code:

//class member function
std::map< std::string, std::string > myMap;

const std::map< std::string, std::string >& bar()
{
   return myMap;
}

void myFunc( std::map< std::string, std::string >& foo1 )
{
   foo1 = bar();
   std::map< std::string, std::string >& foo2 = bar();
}

My understanding is that if I start using foo2, since foo2 is a reference to the same instance as what bar() returns, anything I do with foo2 will be reflected in myMap. But what about foo1? Does foo1 get a copy of myMap or does it also point to the same instance as what bar() returns? The c++ standard library says that the assignment operator for std::map will copy the elements over, but then does that mean the assignment operator is not really invoked in the declaration of foo2?

Thanks!

bhh1988
  • 1,262
  • 3
  • 15
  • 30

2 Answers2

16

References are not reseatable in C++. This means that once they're initialized, you can't reassign them. Instead, any assignment actually involve the referred object. So in your code,

foo1 = bar();
std::map< std::string, std::string >& foo2 = bar();

the first line calls std::map::operator= on the object that was passed as a parameter to myFunc. After that, foo1 stills refers to that same object -- but its value (e.g. what elements it holds) may very well has been changed.

Note that the second line is not assignment if there ever was any doubt that it was in your mind. Instead, it is an initialization. Since the return type of bar is actually std::map<std::string, std::string> const&, it can't bind to std::map<std::string, std::string>& so it's a compile error.


To expand on the 'philosophical' side of things, C++ references are designed to be as transparent as possible and don't really exist as objects. This is using the C++ Standard meaning of the term (it is not related to OOP): it means that for instance reference types do not have a size. Instead, sizeof(T&) == sizeof(T). Similarly, references do not have addresses and it's not possible to form a pointer or a reference to a reference: given int& ref = i;, then &ref == &i.

References are thus purposefully meant to be used as if the referred objects were being used themselves. The only thing reference-specific that happens in the lifetime of a reference is its initialization: what it can bind to and what it means in terms of lifetime.

Luc Danton
  • 34,649
  • 6
  • 70
  • 114
  • +1 Thank you, I learned something new myself. I just made a test and I was indeed absolutely 100% WRONG and you are right =-) – flumpb Jul 31 '11 at 00:42
  • Ok great! Thanks, what I really needed to know was that assignment operator is invoked in foo1 and is not being invoked in foo2. Great explanation, thanks! – bhh1988 Jul 31 '11 at 00:43
  • @Luc If possible, can you go over why reference variables are generally not used? I have a better understanding of what they are (thanks to your answer) but it seems coders generally favor pointers over it. Is it the redundancy? The limited scope? – Manny D Aug 10 '11 at 15:09
  • @MannyD My experience is not similar to yours. I use references and see references used as local variables regularly, and for this usage they are usually preferable over pointers. – Luc Danton Aug 10 '11 at 17:58
  • @Luc I guess I should clarify. I see/use them all the time in function parameter lists but I don't see why you'd want to declare one within a body of code or even as a class member. – Manny D Aug 10 '11 at 18:21
  • @MannyD That's what I meant by 'local variable'. There are several questions on the topic already, I suggest you look there for answers. Here's [something](http://stackoverflow.com/questions/6675651/when-should-i-use-c-pointers-over-smart-pointers/6676183#6676183) I said on the topic (the question is originally about pointers vs smart pointers though). It's likely there also are people in the C++ chat willing to discuss this. (It's a more appropriate place for discussions than here.) – Luc Danton Aug 10 '11 at 18:34
0

The line

foo1 = bar();

creates a copy (because that's what map's assignment operator does).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • seems strange to return a reference to a global variable, no? –  Jul 31 '11 at 00:33
  • @0A0D: Not really, it can be used for encapsulation, if the "global" variable doesn't actually have external linkage. Or you might return a reference to either global variable `a` or global variable `b`. – Ben Voigt Jul 31 '11 at 01:24