title: C++, will this cause memory leak?
body: <p>I know that I can't get a reference of a local var. such as:</p>
  
  <pre><code>int&amp; func1()
  {
      int i;
      i = 1;
      return i;
  }
  </code></pre>
  
  <p>And I know that this is correct, but I have to delete it after calling func2()</p>
  
  <pre><code>int* func2()
  {
      int* p;
      p = new int;
  
  
         *p = 1;
          return p;
  }
  
  int main()
  {
      int *p = func2();
      cout &lt;&lt; *p &lt;&lt; endl;
      delete p;
      return 0;
  }
  </code></pre>
  
  <p><strong>If the function is like this:</strong></p>
  
  <pre><code>MyClass MyFunction()
  {
      return new MyClass;
  }
  </code></pre>
  
  <p>MyClass's whole definition is:</p>
  
  <pre><code>class MyClass
  {
  public:
      MyClass() : num(1){}
      MyClass(MyClass*) : num(10){}
      int num;
  };
  </code></pre>
  
  <p>Will this cause memory leak?</p>
  
  <p><strong>How should I avoid it?
  the function returns an object not a pointer, so how can I delete it?</strong></p>
  
  <p><strong>PS: the code comes from the book "Ruminations on C++" Chapter 10.
  the original code is:</strong></p>
  
  <pre><code>Picture frame(const Pictrue&amp; pic)
  {
      // Picture has a constructor Picture(*P_Node)
      // Frame_Pic derives from P_Node
      // So the constructor Picture(*P_Node) will implicitly convert Frame_Pic to Picture.
      return new Frame_Pic(pic);
  }
  </code></pre>