<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>Atunci, de ce nu:</div><div><br></div><div>mine operator+ (const mine x) {</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>mine y;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>memcpy(y.u, x.u, 10000 * sizeof(int));</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return y;</div><div>}</div><div><br></div><div>Ideea fiind ca la final, y fiind un obiect va fi distrus automat cand iesi din main prin apelul destructorului. Ce faci tu e sa creezi un non-obiect in operator+ si banuiesc ca de-aia nu merge.</div><div><br></div><div>On Mar 6, 2010, at 4:29 PM, marius Dragus wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Pai operatorul + nu ar trebui sa modifice obiectul curent.<br>De exemplu pe stringuri:<br><br>string x ="ana";<br>string y ="are";<br>string z = x+y;<br><br>cout&lt;&lt; x; //afiseaza "ana", 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-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); margin-top: 0pt; margin-right: 0pt; margin-bottom: 0pt; margin-left: 0.8ex; padding-left: 1ex; position: static; z-index: auto; ">
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> &nbsp; &nbsp; &nbsp; &nbsp;memcpy(this-&gt;u, x.u, 10000 * sizeof(int));<br>
 &nbsp; &nbsp; &nbsp; &nbsp;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 "mine" 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; &nbsp; &nbsp; int * u;<br>
&gt; &nbsp; &nbsp; mine()<br>
&gt; &nbsp; &nbsp; {<br>
&gt; &nbsp; &nbsp; u = new int[10000];<br>
&gt; &nbsp; &nbsp; count++;<br>
&gt; &nbsp; &nbsp; }<br>
&gt;<br>
&gt; &nbsp; &nbsp; ~mine()<br>
&gt; &nbsp; &nbsp; {<br>
&gt; &nbsp; &nbsp; delete[] u;<br>
&gt; &nbsp; &nbsp; count--;<br>
&gt; &nbsp; &nbsp; }<br>
&gt;<br>
&gt; &nbsp; &nbsp; mine &amp; operator=(const mine &amp; x)<br>
&gt; &nbsp; &nbsp; {<br>
&gt; &nbsp; &nbsp; //cout &lt;&lt; "assign" &lt;&lt; endl;<br>
&gt; &nbsp; &nbsp; u = x.u;<br>
&gt; &nbsp; &nbsp; }<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; &nbsp; &nbsp; mine * ret = new mine;<br>
&gt; &nbsp; &nbsp; return *ret;<br>
&gt; }<br>
&gt;<br>
&gt; int main()<br>
&gt; {<br>
&gt;<br>
&gt; &nbsp; &nbsp; mine a, b, c, d;<br>
&gt; &nbsp; &nbsp; mine e;<br>
&gt;<br>
&gt;<br>
&gt; &nbsp; &nbsp; for (int i = 0; i&lt;10; i++)<br>
&gt; &nbsp; &nbsp; {<br>
&gt; &nbsp; &nbsp; e = a + b + c + d;<br>
&gt;<br>
&gt; &nbsp; &nbsp; }<br>
&gt;<br>
&gt; &nbsp; &nbsp; cout &lt;&lt; count &lt;&lt; "\n"; // afiseaza 35 desi ar trebui sa fie 5<br>
&gt;<br>
&gt; &nbsp; &nbsp; 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>
_______________________________________________<br>cdl-studenti mailing list<br><a href="mailto:cdl-studenti@lists.rosedu.org">cdl-studenti@lists.rosedu.org</a><br>http://lists.rosedu.org/listinfo/cdl-studenti</blockquote></div><br></body></html>