title: how to return object without create copy of that?
body: <p>I develop simple c++ class to testing when c++ objects destroy; now I have a problem, when an object return by function, c++ create a new object and return that and when return reference destroy object what is my mistake ?</p>
  
  <p>simple class attached below.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  static int freeCounter=0;
  
  class TestCopy {
  private:
      string pStr;
  public:
      TestCopy(const TestCopy &amp;obj){
          pStr=obj.pStr;
      }
      TestCopy(string &amp;test){
          pStr=test;
      }
      ~TestCopy(){ freeCounter++; cout &lt;&lt; freeCounter &lt;&lt;"\t" &lt;&lt; pStr &lt;&lt; endl; }
  
      TestCopy get(){
          TestCopy x=*this; 
          return TestCopy(x); // -&gt; TestCopy(x) is first destroy in result
      }
  
      string getStr(){
          return pStr;
      }
  };
  
  int main(){
      string xstr="test";
      TestCopy x(xstr); // x is third destroy
      TestCopy x2=x.get(); // x2 is second destroy
  
      cout &lt;&lt; x.getStr() &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>and result</p>
  
  <pre><code>1   test
  test
  2   test
  3   test
  </code></pre>