KDE/kdelibs/kdeui

Ingo Klöcker kloecker at kde.org
Mon Sep 15 20:59:36 BST 2008


On Monday 15 September 2008, Michael Jansen wrote:
> Hi
>
> > Now, even more, please note that the function:
> >
> > 	const T &QList<T>::operator[](int i) const
> >
> > when expanding with T = QAction*, expands to:
> >
> > 	QAction * const &QList<QAction *>::operator[](int i) const
> >
> > Note the position of the const: it's 'QAction * const', not 'const
> > QAction *' like Ingo and Michael are thinking.

Uhh. *cough* Right. Where was that brown paper bag?


> Just for the record: "I haven't thought anything. I just copied code.
> Which is much worse :-) .

Thiago knew this. He was talking about me and Michael Pyne. :-p


> Returning an QList<QAction*> means returning a copy of the list (not
> technically - implicit sharing ... - but from the perspective of the
> caller that doesn't matter). If i change the list i got, i won't
> change anything for the original list.
>
> Has the const any kind of effect? Itcould be there to prevent the
> calling code to change the list it gets, but still allow to change
> the actions? So no adding and removing of actions

But why? It's a copy of the list. The calling code should be allowed to 
do whatever it wants with the list. If the code returned a reference to 
the list then it would make sense to add a const.


> Perhaps the idea is then to prevent the caller from thinking: If i
> change the list i got, then the changes are visible to
> kactioncollection?

Any C++ coder should know that he would need a reference to the list for 
this.

Really the only reason for returning a const value is to give the 
compiler a hint for optimization. But I guess the same can be achieved 
by assigning the return value to a const variable (which one should do 
anyway whenever possible).


> So what should i do? We cannot remove the const in KActionCollection.
> But i could remove it from KActionCategory. What is the right thing
> to do here?

Remove it. As Aaron and Thiago have pointed out, there's no reason for 
making the returned list const except for making the return value 
harder to use for everybody.


> > You really want QList<const QAction *>.
>
> No i don't. I don't want to prevent the caller from changing the
> actions.

Sure. But if you wanted to prevent the caller from changing the actions 
then you'd return a QList<const QAction *>.


Regards,
Ingo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20080915/71de7c39/attachment.sig>


More information about the kde-core-devel mailing list