title: What is the scope of object which is passed implicitly and returned with its reference?
body: <p>I am just trying to overload = operator to copy strings.</p>
  
  <pre><code>class cString
  {
   int len;
   char *mbuff;
  };
  
  
  int main()
  {
  cString s1,s2;
  s1 = s2;
  //here s1 goes out of scope and its destructor called
  cout&lt;&lt;" hello";
  ......
  ......
  ......
  return 0;
  }
  
  cString&amp; cString::operator=(const cString &amp;s)
  {
  if(this-&gt;mbuff!=NULL)
     delete[] (this-&gt;mbuff);
  
  this-&gt;len = s.len;
  this-&gt;mbuff = new char[this-&gt;len+1];
  strcpy(this-&gt;mbuff,s.mbuff);
  return *this;
  }
  </code></pre>
  
  <p>s1=s2 is treated as <strong>s1.operator=(s2);</strong></p>
  
  <p>In <strong>operator=</strong> function s1 is passed implicitely.
  As s1 is created in main's block s1's <strong>destructor</strong> should be called at the end. i.e Just before exiting main.</p>
  
  <p>But If I write return type <strong>cString&amp;(reference)</strong> s1's <strong>destructor</strong> is called immediately after returning from function to main. 
  But if return type is <strong>void</strong> it doesn't call s1's <strong>destructor</strong> immediately. It calls normally
  while exiting main..</p>
  
  <p>Why does object <strong>s1</strong> go out of scope when I return reference? What's the exact meaning of return *this?</p>
  
  <p><strong>I know that there is no need to return a reference. I have executed my code successfully with void return type. I am just curious about what might be happening...!</strong></p>
  
  <p><strong>Thanks...!</strong></p>