[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: Problem with QT
From:       Sascha Cunz <sascha.cunz () tiscali ! de>
Date:       2005-10-22 6:36:43
Message-ID: 200510220836.44123.sascha.cunz () tiscali ! de
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hi,
Am Samstag 22 Oktober 2005 09:27 schrieb Bernd Brandstetter:
> On Saturday 22 October 2005 00:46, David Faure wrote:
> > > And for a perhaps more useful answer, add a destructor for
> > > KgpgListKeys that iterates over its keys and deletes them.  You might
> > > not be able to use foreach() since the dtor will be iterating over
> > > itself but something like this would work:
> > >
> > > KgpgListKeys::~KgpgListKeys()
> > > {
> > >   for(ConstIterator it = constBegin(); it != constEnd(); ++it)
> > >   {
> > >     delete *it;
> > >   }
> >
> > qDeleteAll(this) should do the same as the above code.
>
> But be very careful when putting this in the destructor. If somebody
> iterates this list using foreach(), he will end up with all elements being
> deleted after the loop - which is most probably not what he expected.
Unless you talk about multithreading, what you say, would look in code like:
	foreach( T t, *list ) if( t.foo ) delete list;
I'd say: If somebody does that, well then he has to expect the list to get invalid.
Cheers Sascha
[Attachment #5 (text/html)]

<html><head><meta name="qrichtext" content="1" /></head><body \
style="font-size:14pt;font-family:Terminal [DEC]"> <p>Hi,</p>
<p>Am Samstag 22 Oktober 2005 09:27 schrieb Bernd Brandstetter:</p>
<p><span style="color:#008000">&gt; On Saturday 22 October 2005 00:46, David Faure \
wrote:</span></p> <p><span style="color:#006000">&gt; &gt; &gt; And for a perhaps \
more useful answer, add a destructor for</span></p> <p><span \
style="color:#006000">&gt; &gt; &gt; KgpgListKeys that iterates over its keys and \
deletes them.  You might</span></p> <p><span style="color:#006000">&gt; &gt; &gt; not \
be able to use foreach() since the dtor will be iterating over</span></p> <p><span \
style="color:#006000">&gt; &gt; &gt; itself but something like this would \
work:</span></p> <p><span style="color:#006000">&gt; &gt; &gt;</span></p>
<p><span style="color:#006000">&gt; &gt; &gt; \
KgpgListKeys::~KgpgListKeys()</span></p> <p><span style="color:#006000">&gt; &gt; \
&gt; {</span></p> <p><span style="color:#006000">&gt; &gt; &gt;   for(ConstIterator \
it = constBegin(); it != constEnd(); ++it)</span></p> <p><span \
style="color:#006000">&gt; &gt; &gt;   {</span></p> <p><span \
style="color:#006000">&gt; &gt; &gt;     delete *it;</span></p> <p><span \
style="color:#006000">&gt; &gt; &gt;   }</span></p> <p><span \
style="color:#007000">&gt; &gt;</span></p> <p><span style="color:#007000">&gt; &gt; \
qDeleteAll(this) should do the same as the above code.</span></p> <p><span \
style="color:#008000">&gt;</span></p> <p><span style="color:#008000">&gt; But be very \
careful when putting this in the destructor. If somebody</span></p> <p><span \
style="color:#008000">&gt; iterates this list using foreach(), he will end up with \
all elements being</span></p> <p><span style="color:#008000">&gt; deleted after the \
loop - which is most probably not what he expected.</span></p> <p>Unless you talk \
about multithreading, what you say, would look in code like:</p> <p>	foreach( T t, \
*list ) if( t.foo ) delete list;</p> <p>I'd say: If somebody does that, well then he \
has to expect the list to get invalid.</p> <p>Cheers Sascha</p>
</body></html>



>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic