Note that there are some explanatory texts on larger screens.

plurals
  1. POElement is removed from QList but static counter of existing objects doesn't decrease
    text
    copied!<p>I have question about removing element from QList.</p> <p>"myclass.h":</p> <pre><code>class node2D : public QObject { Q_OBJECT public: node2D(){++s_NCount;}; ~node2D(){--s_NCount;}; int checkCount(){return s_NCount;}; private: static int s_NCount; }; </code></pre> <p>"myclass.cpp":</p> <pre><code>int node2D::s_NCount = 0; </code></pre> <p>"main.cpp":</p> <pre><code>void main() { int i,max_el(4); QList&lt;node2D*&gt; *Nlist; Nlist = new QList&lt;node2D*&gt;; node2D controlNode; for (i = 0 ;i &lt; max_el ; i++) { Nlist-&gt;append(new node2D); } cout &lt;&lt; "Nlist size before: " &lt;&lt; Nlist-&gt;size() &lt;&lt; endl; cout &lt;&lt; "Number of nodes before removing: " &lt;&lt; controlNode.checkCount() &lt;&lt; endl; Nlist-&gt;clear(); cout &lt;&lt; "NList size after: " &lt;&lt; Nlist-&gt;size() &lt;&lt; endl; delete Nlist; cout &lt;&lt; "Number of nodes after removing: " &lt;&lt; controlNode.checkCount() &lt;&lt; endl; } </code></pre> <p>After executing I get:</p> <ul> <li>NList size before: 4</li> <li>Number of nodes before removing: 5</li> <li>NList size after: 0</li> <li>Number of nodes after removing: 5</li> </ul> <p>What's bothering me is the fact that number of node2D objects is still 5 instead of 1. </p> <p>Of course it can be managed like this:</p> <pre><code>for (i = 0; i &lt; Nlist-&gt;size(); i++) { delete (*Nlist)[i]; } Nlist-&gt;clear(); </code></pre> <p>but shouldn't node2D objects be automatically deleted while Nlist->clear()? </p> <p><strong>Or does it only happen when there is parent-child relation?</strong></p> <p>Thanks in advance,</p> <p>Pawel</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload