title: Operators overloading : memory leaks
body: <p>This is something I can't understand.
  Let's say that I overload operator &amp; to make and and between two objects, and operator = to make assignments (making a copy of the object).
  Let's suppose that I have a class named A, and this code:</p>
  
  <pre><code>A a1,a2,a3;
  // initialize a1,a2,a3
  a1=a2&amp;a3;
  </code></pre>
  
  <p>If a1 was already allocated, when I assign a1 to a2&amp;a3, there is a memory leak?
  is the new object created without deleting the old one ?</p>
  
  <p>PS: Operators overloading using two arguments of type A (I haven't defined a class A, this is an example), returning an arguments of type A, defined as friend of the class.</p>
  
  <p>The code:</p>
  
  <p>It's a very long code, so I used pastebin:
  <a href="http://pastebin.com/42TnThfC" rel="nofollow">http://pastebin.com/42TnThfC</a>
  But since it's long, I also post the most significative parts:</p>
  
  <pre><code>template&lt;class T&gt;
  List&lt;T&gt;&amp; List&lt;T&gt;::operator= (List&lt;T&gt;&amp; l)
  {
      List&lt;T&gt;* ptr=l.next;
      if(this!=&amp;l)
      {
          resize(0);
          while(!ptr-&gt;end)
          {
              push(ptr-&gt;info);
              ptr=ptr-&gt;next;
          }
      }
      return *this;
  }
  
  template &lt;class T&gt;
  List&lt;T&gt;&amp; operator&amp; (List&lt;T&gt;&amp; l1, List&lt;T&gt;&amp; l2) throw()
  {
      List&lt;T&gt;* temp,*ptr=l1.next;
      temp=new List&lt;T&gt;();
      try
      {
          if( (l1.end)^(l2.end) )
              throw excp1;
      }
      catch (char *s)
      {
          cout &lt;&lt; "Exception: "&lt;&lt;s&lt;&lt;endl;
      }
      if(l1.end)
      {
          while(!ptr-&gt;end)
          {
              if(l2.in(ptr-&gt;info))
                  temp-&gt;push(ptr-&gt;info);
              ptr=ptr-&gt;next;
          }
      }
      else
      {
          if(l1.info==l2.info)
              temp-&gt;push(l1.info);
      }
      return *temp;
  }
  </code></pre>
  
  <p>It's a list, push pushes an item, an example of main:</p>
  
  <pre><code>int main(int arcg, char **argv)
  {
      List&lt;T&gt;l1,l2,l3;
      for(int i=0;i&lt;10;i++)
      {
          l1.push(i);
          l2.push(i);
          l3.push(i);
      }
      l3=l1&amp;l2;
  }
  </code></pre>
  
  <p>This case l3 is already l1&amp;l2, but will this cause a memory leak?</p>