Deleting non used objects in operator= overloading
<p>Let's say that I have a class A:</p>

<pre><code>class A
{
    private:
    int value;
    public:
    A(int value)
    {
        this-&gt;value=value;
    }
    A&amp; operator= (A&amp; obj)
    {
        value=obj.value;
    }
};
</code></pre>

<p>There is a problem if I use the contextual instantiation:</p>

<pre><code>A obj;
obj=*new A(5);
</code></pre>

<p>This case the new object created is not deleted and I lose it's reference.
So I could put in the operator= overloading function the instruction to delete the object which is right value of the assignment, but if I do this:</p>

<pre><code>A obj1,obj2;
obj1=obj2;
</code></pre>

<p>So how to avoid to waste memory? There's a way?</p>

<p>@Oli Charlesworth :
But then how are iterators implemented? For example in std::list if I say:</p>

<pre><code>list&lt;string&gt; l;
// initialize the list elements
list&lt;string&gt;::iterator i=l.begin();
</code></pre>

<p>This is an assignment, is another object of kind list::iterator created?</p>
