title: Why is my string reference member variable set to an empty string in C++?
body: <p>Consider the following code:</p>
  
  <pre><code>class Foo
  {
  private:
      const string&amp; _bar;
  
  public:
      Foo(const string&amp; bar)
          : _bar(bar) { }
  
      const string&amp; GetBar() { return _bar; }
  };
  
  int main()
  {
      Foo foo1("Hey");
      cout &lt;&lt; foo1.GetBar() &lt;&lt; endl;
  
      string barString = "You";
      Foo foo2(barString);
      cout &lt;&lt; foo2.GetBar() &lt;&lt; endl;
  }
  </code></pre>
  
  <p>When I execute this code (in VS 2013), the <code>foo1</code> instance has an empty string in its <code>_bar</code> member variable while <code>foo2</code>'s corresponding member variable holds the reference to value "You". <strong>Why is that?</strong></p>
  
  <p>Update: I'm of course using the std::string class in this example.</p>