title: Returning a temporary object and using its reference
body: <p>Say I have this method that creates an object of type <code>std::vector&lt; std::string &gt;</code></p>
  
  <pre><code>const std::vector&lt; std::string &gt; Database::getRecordNames() {
      // Get the number of recors
      int size = this -&gt; getRecordCount();
  
      // Create container
      std::vector&lt; std::string &gt; names;
  
      // Get some strings
      for ( i = 0; i &lt; size; i++ ) {
          // Get a string
          const std::string &amp; name = this -&gt; getName( i );
  
          // Add to container
          names.push_back( name );
      }
  
      // Return the names
      return names;
  }
  </code></pre>
  
  <p>And then somewhere else, I use this method</p>
  
  <pre><code>void Game::doSomething() {
      const std::vector&lt; std::string &gt; &amp; names = mDatabase -&gt; getRecordNames();
  
      // Do something about names
  }
  </code></pre>
  
  <p>So, on the method <code>Database::getRecordNames()</code>, it returns a temporary object <code>std::vector&lt; std::string &gt;</code>. However, on the method <code>Game::doSomething()</code>, I placed the return value to a  <code>const std::vector&lt; std::string &gt; &amp;</code>-type object.</p>
  
  <p>Is this unsafe, or is it perfectly normal to use them like this? AFAIK, temporary variables are destroyed on the end of their scope. But in our case, we reference this temporary variable, in which I believe will be destroyed after it returns the value.</p>
  
  <p>Is it better to rewrite the other method so that it would use a copy of the returned value instead of a reference?</p>
  
  <pre><code>void Game::doSomething() {
      const std::vector&lt; std::string &gt; names = mDatabase -&gt; getRecordNames();
  
      // Do something about names
  }
  </code></pre>