title: How to properly return std::string (or how to properly use that returned value)
body: <p>Say you have a class which is a global (e.g. available for the runtime of the app)</p>
  <pre><code>class MyClass {
    protected:
      std::string m_Value;
    public:
      MyClass () : m_Value(&quot;hello&quot;) {}
      std::string value() { return m_Value; }      
  };
  
  MyClass v1;
  </code></pre>
  <p>Using the first form gives me odd behavior when I do</p>
  <pre><code>printf(&quot;value: %s\n&quot;, v1.value().c_str());
  </code></pre>
  <p>It looks as though the string disappears from memory before printf can use it.
  Sometimes it prints value: hello other times it crashes or prints nothing.</p>
  <p>If I first copy the string like so</p>
  <pre><code>   std::string copiedString = v1.value();
     printf(&quot;value: %s\n&quot;, copiedString.c_str());
  </code></pre>
  <p>things do work.</p>
  <p>Surely there must be a way to avoid doing this with a temporary string.</p>
  <p>Edit: So the consensus is to go with a const std::string &amp; return value.</p>
  <p>I know everyone says that the original code should be fine but I can tell you that I've seen MSVC 2005 on Windows CE having trouble with it, but only on the CE box. Not the Win32 cross compile.</p>