[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">> On Saturday 22 October 2005 00:46, David Faure \
wrote:</span></p> <p><span style="color:#006000">> > > And for a perhaps more useful \
answer, add a destructor for</span></p> <p><span style="color:#006000">> > > \
KgpgListKeys that iterates over its keys and deletes them. You might</span></p> <p><span \
style="color:#006000">> > > not be able to use foreach() since the dtor will be \
iterating over</span></p> <p><span style="color:#006000">> > > itself but something \
like this would work:</span></p> <p><span style="color:#006000">> > ></span></p>
<p><span style="color:#006000">> > > KgpgListKeys::~KgpgListKeys()</span></p>
<p><span style="color:#006000">> > > {</span></p>
<p><span style="color:#006000">> > > for(ConstIterator it = constBegin(); it != \
constEnd(); ++it)</span></p> <p><span style="color:#006000">> > > {</span></p>
<p><span style="color:#006000">> > > delete *it;</span></p>
<p><span style="color:#006000">> > > }</span></p>
<p><span style="color:#007000">> ></span></p>
<p><span style="color:#007000">> > qDeleteAll(this) should do the same as the above \
code.</span></p> <p><span style="color:#008000">></span></p>
<p><span style="color:#008000">> But be very careful when putting this in the destructor. If \
somebody</span></p> <p><span style="color:#008000">> iterates this list using foreach(), he \
will end up with all elements being</span></p> <p><span style="color:#008000">> 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