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

List:       kde-devel
Subject:    Re: QListIterator and const T* to T* assignment
From:       David Jarvie <lists () astrojar ! org ! uk>
Date:       2006-06-01 10:17:28
Message-ID: 86b360b089ed3dcac47bb4025c669329 () webmail ! plus ! net
[Download RAW message or body]

On Thursday 1 Jun 2006 10:40, Dmitry Suzdalev wrote:
>Hi! Please, take a look at this patch.
>It replaces Q3PtrList with QList in libkdegames' kplayer.{cpp,h}
>
>I have a few question about QListIterator usage.
>
>First of all I'm feeling stupid again ;). Ah, what a feeling!:)
>
>QListIterator::next() returns const reference to T, so in this case
>it's prototype looks like:
>
>const KGameIO* & QListIterator::next()
>
>How this can be that gcc accepts the following assignment:
>
>KGameIO* curGameIO = it.next()
>
>?
>
>curGameIO is non-const!
>I've written a small testcase using ints and it doesn't pass...
>What am I missing?

AFAICS, the reason is that you're creating a COPY of the pointer in the list, which 
doesn't give you any means of changing the actual contents of the list. Remember 
that the list contents are only the pointers, not what the pointers point to. So the 
list isn't changed if you change the KGameIO instances, only if you change the 
pointers in the list to point to other KGameIO instances.

What you would NOT be allowed to do by the compiler is:

KGameIO* & curGameIO = it.next();

since changing curGameIO now would change the actual list contents.

--
David Jarvie.
 
>> 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