Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yeah, <code>string operator+ (const char* lhs, const string&amp; rhs)</code> creates and returns an unnamed temporary string object. </p> <p><code>cout &lt;&lt; "Some text"</code> calls ostream&amp; ostream::operator&lt;&lt;( const char* const) and returns the ostream reference.</p> <pre><code>cout &lt;&lt; "Some text" &lt;&lt; s1 &lt;&lt; "some more text\n"; </code></pre> <p>is <code>ostream::operator&lt;&lt;( "Some text").operator&lt;&lt;(s1).operator&lt;&lt;("some more text\n")</code>, three calls on cout, calling two different overloads of op&lt;&lt;, the op&lt;&lt; that takes a <code>const* char* const</code> and the op&lt;&lt; that takes a <code>const string&amp;</code>. No memory allocations, and the only copying is to cout's buffer.</p> <pre><code>cout &lt;&lt; "Some text" + s1 + "some more text\n"; </code></pre> <p>is <code>ostream::operator&lt;&lt;(t1)</code> on a temporary string object I'll call <em>t1</em>, which t1 is the temporary result from <code>::operator+ ( "Some text", s1).operator+("some more text\n")</code>. (Note that the first operator+ is not a member of string, but the op+ (const char* const, const string&amp;) at namespace scope. It returns a string, so the second +" is string::operator+( const char* const).) </p> <p>So two temporary string objects are created (and then destructed), in this code. That means two internal representations for the strings (so two memory allocations) and two copies two that newly allocated memory (in addition to the copying to cout's buffer).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload