gcc -Woverloaded-virtual

Matthias Kretz kretz at kde.org
Wed Nov 21 15:42:08 GMT 2007


On Wednesday 21 November 2007, David Faure wrote:
> On Wednesday 21 November 2007, Matthias Kretz wrote:
> > On Wednesday 21 November 2007, David Faure wrote:
> > > What we're missing is a way to say "yes I wanted to hide this method
> > > from the base class"...
> >
> > Hiding a method from a base class is not possible anyway as you can still
> > cast down and then call the method. It kind of works for value based
> > classes since those are normally not casted down, but for all
> > pointer-based (i.e. QObject) classes I believe you should not hide a
> > method and get a warning if you do (also for non-virtual functions).
>
> Well that's the theory :)
> In practice the base class method doesn't always make sense. Example:
>
> /d/kde/src/4/kdelibs/kdeui/actions/kselectaction.h:217: warning: ‘virtual
> void KSelectAction::addAction(QAction*)’ was hidden
> /d/kde/src/4/kdelibs/kdeui/actions/krecentfilesaction.h:98: warning:   by
> ‘void KRecentFilesAction::addAction(QAction*, const KUrl&, const QString&)’
>
> Sure you could call KSelectAction::addAction even on a KRecentFilesAction,
> but you don't want to... you want to associate a url with the action. This
> is documented, too. /**
>    * Adds \a action to the list of URLs, with \a url and title \a name.
>    *
>    * Do not use addAction(QAction*), as no url will be associated, and
>    * consequently urlSelected() will not be emitted when \a action is
> selected. */
>   void addAction(QAction* action, const KUrl& url, const QString& name);
>
> So here the hiding is good... it's actually an example of why the hiding
> feature exists in the first place...

Yes, and the nice thing for the developer using the API would be to implement
KRecentFilesAction::addAction(QAction *)
{
    kFatal/kWarning(...) << "some helpful message";
}

Sure this comes with an extra cost (bigger libs, more symbols).


Hmm, how about this:

class KRecentFilesAction : public Whatever
{
...
private:
    void addAction(QAction *) {}
...
};

or

#include <kdebug.h>
class KRecentFilesAction : public Whatever
{
...
public:
    void addAction(QAction *) { kWarning() << "explanation"; }
...
};

-- 
________________________________________________________
Matthias Kretz (Germany)                            <><
http://Vir.homelinux.org/
MatthiasKretz at gmx.net, kretz at kde.org,
Matthias.Kretz at urz.uni-heidelberg.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20071121/0975c8f3/attachment.sig>


More information about the kde-core-devel mailing list