Pai operatorul + nu ar trebui sa modifice obiectul curent.<br>De exemplu pe stringuri:<br><br>string x =&quot;ana&quot;;<br>string y =&quot;are&quot;;<br>string z = x+y;<br><br>cout&lt;&lt; x; //afiseaza &quot;ana&quot;, x nu e modificat.<br>

<br><br><div class="gmail_quote">2010/3/6 Dan-George Filimon <span dir="ltr">&lt;<a href="mailto:dangeorge.filimon@gmail.com">dangeorge.filimon@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Nu inteleg de ce ai in definitia operatorului + o noua alocare de memorie.<br>
De ce nu folosesti pointerul this?<br>
<br>
Cand faci un operator ca + m-as gandi ca ai ceva de genul:<br>
<div class="im">mine operator+(const mine x) {<br>
</div>        memcpy(this-&gt;u, x.u, 10000 * sizeof(int));<br>
        return *this;<br>
}<br>
<br>
Asta nu ar face ce vrei?<br>
<div><div></div><div class="h5"><br>
On Mar 6, 2010, at 4:20 PM, marius Dragus wrote:<br>
<br>
&gt; Salut,<br>
&gt;<br>
&gt; Incerc sa scriu o clasa &quot;mine&quot; in C++ care sa permita operatorul +.<br>
&gt; Problema este ca fiecare obiect aloca dinamic un array, iar o operatie de genul x =a +b+c ar cauza memory leak-uri.<br>
&gt;<br>
&gt; Cum s-ar putea face asta?<br>
&gt;<br>
&gt; Mai jos este codul. Mentin un contor al constructorilor/destructorilor, si se vede evident ca se obtin memory leakuri.<br>
&gt;<br>
&gt; Mersi,<br>
&gt; Marius<br>
&gt;<br>
&gt; /*************************CODE*********************/<br>
&gt;<br>
&gt; #include &lt;iostream&gt;<br>
&gt;<br>
&gt; int count = 0;<br>
&gt; using namespace std;<br>
&gt;<br>
&gt; struct mine<br>
&gt; {<br>
&gt;     int * u;<br>
&gt;     mine()<br>
&gt;     {<br>
&gt;     u = new int[10000];<br>
&gt;     count++;<br>
&gt;     }<br>
&gt;<br>
&gt;     ~mine()<br>
&gt;     {<br>
&gt;     delete[] u;<br>
&gt;     count--;<br>
&gt;     }<br>
&gt;<br>
&gt;     mine &amp; operator=(const mine &amp; x)<br>
&gt;     {<br>
&gt;     //cout &lt;&lt; &quot;assign&quot; &lt;&lt; endl;<br>
&gt;     u = x.u;<br>
&gt;     }<br>
&gt;<br>
&gt;<br>
&gt; };<br>
&gt;<br>
&gt; mine&amp; operator+(const mine &amp; x, const mine &amp;y)<br>
&gt; {<br>
&gt;     mine * ret = new mine;<br>
&gt;     return *ret;<br>
&gt; }<br>
&gt;<br>
&gt; int main()<br>
&gt; {<br>
&gt;<br>
&gt;     mine a, b, c, d;<br>
&gt;     mine e;<br>
&gt;<br>
&gt;<br>
&gt;     for (int i = 0; i&lt;10; i++)<br>
&gt;     {<br>
&gt;     e = a + b + c + d;<br>
&gt;<br>
&gt;     }<br>
&gt;<br>
&gt;     cout &lt;&lt; count &lt;&lt; &quot;\n&quot;; // afiseaza 35 desi ar trebui sa fie 5<br>
&gt;<br>
&gt;     return 0;<br>
&gt; }<br>
&gt;<br>
</div></div>&gt; _______________________________________________<br>
&gt; cdl-studenti mailing list<br>
&gt; <a href="mailto:cdl-studenti@lists.rosedu.org">cdl-studenti@lists.rosedu.org</a><br>
&gt; <a href="http://lists.rosedu.org/listinfo/cdl-studenti" target="_blank">http://lists.rosedu.org/listinfo/cdl-studenti</a><br>
<br>
_______________________________________________<br>
cdl-studenti mailing list<br>
<a href="mailto:cdl-studenti@lists.rosedu.org">cdl-studenti@lists.rosedu.org</a><br>
<a href="http://lists.rosedu.org/listinfo/cdl-studenti" target="_blank">http://lists.rosedu.org/listinfo/cdl-studenti</a><br>
</blockquote></div><br>