title: Does using references instead of pointers, resolve memory leaks in C++?
body: <p>Most of memory leaks appear when a pointer of an object returned and programmer forgot to delete it.</p>
  
  <p>for example:</p>
  
  <pre><code>class my_class
  {
    ...
  };
  
  my_class* func1()
  {
    my_class* c = new my_class;
    return c;
  }
  
  int main()
  {
    my_class* var1 = func1();
    ...
    // Programmer forgot delete the var1: delete var1;
    // -- or --
    // Doesn't know 'delete[] var1;' is correct or 'delete var1;'.
  }
  </code></pre>
  
  <p>Some of memory leaks appear when a pointer to an object created and programmer forgot to delete it.</p>
  
  <p>for example:</p>
  
  <pre><code>class my_class
  {
    ...
  };
  
  void func2(my_class* p)
  {
    ...
  }
  
  int main()
  {
    my_class* var3 = new my_class;
  
    func2(var3);
  
    // Does func2 deletes var3? Programmer doesn't know.
    // -- or --
    // Programmer forgot delete the var3.
  }
  </code></pre>
  
  <p>I use a method to resolve memory leaks but I don't sure about it in complex situations.</p>
  
  <p>My method is: <strong>Don't use any pointers (except one place), Just use references instead of pointers.</strong></p>
  
  <p>for example:</p>
  
  <pre><code>class my_class
  {
    ...
  };
  
  my_class&amp; func1()
  {
    my_class* c = new my_class; // except one place.
    return *c;
  }
  
  void func2(my_class&amp; p)
  {
    ...
  }
  
  int main()
  {
    my_class&amp; var1 = func1();
    my_class  var2 = func1();
  
    my_class var3;
    func2(var3);
  
    // There is nothing to forget.
  }
  </code></pre>
  
  <p>Does using references instead of pointers, resolve memory leaks?</p>
  
  <p>Is it a good method for resolving memory leaks or there are better methods?</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>Some answer of this question don't agree the below code don't have memory leak.</p>
  
  <p>because it is a new question, I ask it seperately.</p>
  
  <pre><code>class my_class
  {
    ...
  };
  
  my_class&amp; func()
  {
    my_class* c = new my_class;
    return *c;
  }
  
  int main()
  {
    my_class&amp; var1 = func();
  
    // I think there is no memory leak.
  }
  </code></pre>
  
  <p>I ask it here:
  <a href="https://stackoverflow.com/questions/6784152/does-this-code-has-leak-memory">Does this code leak memory? (references, new, but no delete)</a></p>