Use of const arg in KConfigGroup constructors doesn't work with language bindings

Lubos Lunak l.lunak at suse.cz
Wed Mar 5 08:02:49 GMT 2008


On Tuesday 04 of March 2008, Richard Dale wrote:
> I've had a problem with these two pairs of constructors in kconfiggroup.h:
>
>     KConfigGroup(KConfigBase *master, const QString &group);
>     KConfigGroup(KConfigBase *master, const char *group);
>
>      KConfigGroup(const KConfigBase *master, const QString &group);
>     KConfigGroup(const KConfigBase *master, const char *group);
>
> They only differ by the const-ness of the first argument, and this is
> impossible to get working with any other language than c++. I would prefer
> an enum to be passed, like KConfigBase::AccessType to specify whether or
> not the KConfigGroup can be written too. I've worked round the problem by
> just ignoring the read-only const method calls, and so bindings will always
> get a writable KConfigGroup.

 If that's the only way to get it working with the bindings, then that's 
probably the way you need to go.

> I think something like this is clearer and less error prone:
>
>     KConfigGroup(KConfigBase *master, const QString &group, AccessType mode
> = ReadOnly);
>     KConfigGroup(KConfigBase *master, const char *group, AccessType mode =
> ReadOnly);

 No. In fact it is less clear and more error prone. Using 'const' is a normal 
C++ way and it lets the language to handle constness automatically. The code 
you posted will not work (compile) if the KConfigBase is already const and it 
forces the developer to do manually what the compiler can do on its own (and 
the compiler is not human, so unlike the developer it should not do 
mistakes :) ). It is rather common to overload methods in C++ based on 
constness and I'm surprised this is the first time you've run into it.

-- 
Lubos Lunak
KDE developer
--------------------------------------------------------------
SUSE LINUX, s.r.o.   e-mail: l.lunak at suse.cz , l.lunak at kde.org
Lihovarska 1060/12   tel: +420 284 028 972
190 00 Prague 9      fax: +420 284 028 951
Czech Republic       http//www.suse.cz




More information about the kde-core-devel mailing list