[Qt-interest] Problem with Base Class Method in Derived Class

Thiago Macieira thiago.macieira at trolltech.com
Thu May 7 08:20:09 CEST 2009


Shimaron Greywolf wrote:
>Here's one strange problem - probably not really a Qt one, but
>probably someone can help.
>
>I thought I had my share of C++ design and coding, but the following
>was new to me. Consider this code:

>Why isn't some_method(const QString) resolved properly?

Yes, this is actually a C++ problem. Here's why:

$ /usr/bin/g++ -I$QTDIR/include -fsyntax-only -Woverloaded-virtual -xc++ 
/dev/stdin
#include <QtCore/QString>
class x
{
public:
     virtual ~x();
     virtual void some_method(const QString i);
};

class xx : public x
{
public:
     virtual void some_method(const int i);
     virtual ~xx();
};
/dev/stdin:6: warning: ‘virtual void x::some_method(QString)’ was hidden
/dev/stdin:12: warning:   by ‘virtual void xx::some_method(int)’

The -Woverloaded-virtual tells you why. Unfortunately, that option also 
produces a lot of false positives, so it's not enabled by default on -
Wall.

If you want to "see" the parent's method, add to xx:
	using x::some_method;

Some old compilers (MSVC 6 comes to mind) don't support this syntax. 
You'll need:
	inline void some_method(const QString i)
	{ x::some_method(i); }

PS: also note that the compiler dropped the "const" from your methods.
-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Product Manager - Nokia, Qt Software
      Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090507/d997fada/attachment.bin 


More information about the Qt-interest-old mailing list