title: Returning reference from overloaded operatora and expression with temporary objects
body: <p>I am playing around with C++ to remind myself about it. So I experimented with operator + overloading returning reference. Motive for this is to avoid unnecessary copying of objects. Look at the example. I created class String and concatenated strings with +. It was just experimental so you'll notice some ugly stuff as public attributes.</p>
  
  <p>Here are relevant parts of the code.</p>
  
  <p>String.hpp</p>
  
  <pre><code>#ifndef STRING_HPP_
  #define STRING_HPP_
  
  #include &lt;iostream&gt;
  
  using namespace std;
  
  #ifndef CPP11
      typedef wchar_t unicode16;
  #else
      typedef char16_t unicode16;
  #endif //CPP11
  
  class String {
  
      unicode16 * value;
      unsigned strLength;
  
      void initEmpty() {
          value = 0L;
          strLength = 0;
      }
  
      static unsigned counter;
  public:
  
      static String ** trash;
  
      unsigned id;
  
      String::String() : value(0L), strLength(0){
          id=counter++;
          trash[id]=this;
          cout &lt;&lt; "Creating empty: " &lt;&lt; id &lt;&lt; "\n";
      }
  
      String::String(const String &amp;str);
  
      String(const char *);
  
      String(const unicode16 *);
  
      unsigned length() const {
          return strLength;
      }
  
      ~String() {
  
          wcout &lt;&lt; L"Deleting " &lt;&lt; id &lt;&lt; ": " &lt;&lt; value &lt;&lt; L"\n";
          trash[id]=0L;
          delete value;
      }
  
      String &amp; operator +(String &amp;);
  
      unicode16 * getValue() {
          return value;
      }
  };
  
  #endif /* STRING_HPP_ */
  </code></pre>
  
  <p>String.cpp</p>
  
  <pre><code>#include "String.hpp"
  #include "../exception/IllegalArgumentException.h"
  #include &lt;string.h&gt;
  
  
  unsigned String::counter = 0;
  
  String ** String::trash = new String *[100]();
  
  String::String(const String &amp; str) {
      value = new unicode16[str.strLength + 1];
      strLength = str.strLength;
      for(int i = 0; i &lt; strLength ; i++) {
          value[i] = str.value[i];
      }
      value[strLength] = 0;
      id = counter++;trash[id]=this;
      wcout &lt;&lt; L"Created (copy): " &lt;&lt; id &lt;&lt; ": " &lt;&lt; value &lt;&lt; L"\n";
  }
  
  String::String(const char *charArray) {
      if (charArray == 0L) {
          throw IllegalArgumentException("Char array pointer is null");
      }
      strLength = strlen(charArray);
      value = new unicode16[strLength + 1];
  
      for (int i = 0; i &lt; strLength; i++) {
          value[i] = (unicode16)charArray[i];
      }
      value[strLength] = 0;
      id = counter++;trash[id]=this;
      wcout &lt;&lt; L"Created (char *): " &lt;&lt; id &lt;&lt; ": " &lt;&lt; value &lt;&lt; L"\n";
  }
  
  String::String(const unicode16 *utfArray) {
      if (utfArray == 0L) {
          throw IllegalArgumentException("Unicode array pointer is null");
      }
      strLength = wcslen(utfArray);
      value = new unicode16[strLength + 1];
  
      for (int i = 0; i &lt; strLength; i++) {
          value[i] = utfArray[i];
      }
      value[strLength] = 0;
      id = counter++;
      trash[id]=this;
      wcout &lt;&lt; L"Created (unicode): " &lt;&lt; id &lt;&lt; ": " &lt;&lt; value &lt;&lt; L"\n";
  }
  
  String &amp; String::operator +(String &amp;str) {
      unsigned newLength = length() + str.length();
      unicode16 * newArray = new unicode16[newLength + 1];
      wcscpy(newArray, value);
      wcscpy(newArray + strLength, str.value);
  
      String * strPointer = new String();
      strPointer-&gt;value = newArray;
      strPointer-&gt;strLength = newLength;
  
      String &amp;result = *strPointer;
      wcout &lt;&lt; L"Empty loaded: " &lt;&lt; result.id &lt;&lt; ": " &lt;&lt; result.value &lt;&lt; L"\n";
      return result;
  }
  </code></pre>
  
  <p>And the main method</p>
  
  <pre><code>#include "../string/string.hpp"
  #include &lt;iostream&gt;
  
  using namespace std;
  
  
  
  int metodica(void) {
      String &amp; please = String("Please");
      String meString = "me";
      String &amp; me = meString;
      String &amp; delStrRef = String(" delete ");
  
      String &amp; result1 = please + delStrRef + me;
  
      wcout &lt;&lt; result1.getValue() &lt;&lt; L"\n";
      delete &amp;result1;
      return 0;
  }
  
  
  int main(void) {
      metodica();
      cout &lt;&lt; "These are not deleted\n";
      for (int i = 0; i &lt; 100; i++) {
          if (String::trash[i] != 0L) {
              wcout &lt;&lt; String::trash[i]-&gt;getValue() &lt;&lt; "\n";
          }
      }
  }
  </code></pre>
  
  <p>Executing this in CDT using VS2010 compiler and linker i got following output</p>
  
  <p>Created (char *): 0: Please</p>
  
  <p>Created (char *): 1: me</p>
  
  <p>Created (char *): 2:  delete </p>
  
  <p>Creating empty: 3</p>
  
  <p>Empty loaded: 3: Please delete </p>
  
  <p>Creating empty: 4</p>
  
  <p>Empty loaded: 4: Please delete me</p>
  
  <p>Please delete me</p>
  
  <p>Deleting 4: Please delete me</p>
  
  <p>Deleting 2:  delete </p>
  
  <p>Deleting 1: me</p>
  
  <p>Deleting 0: Please</p>
  
  <p>These are not deleted</p>
  
  <p>Please delete </p>
  
  <p>The question is why temporary object created in the expression please + delStrRef + me; is not deleted. Shouldn't it get deleted at the end of the expression or it goes differently if reference is temporary object and not the object itself.</p>