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

Shimaron Greywolf shimaron at furry.de
Thu May 7 07:15:46 CEST 2009


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:

===== test.h

#include <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();
};

====== test.cpp

#include <QDebug>
#include "test.h"
int main()
{
     xx * yy = new xx();
     yy -> some_method((int)65);
     yy -> some_method(QString("foo"));
}



x::~x() {}
void x::some_method(const QString i) {qDebug()<<"x::QString";}
void xx::some_method(const int i) {qDebug()<<"xx::int";}
xx::~xx() {}


=========

My expectation was that in the new xx object, I'd have access to the  
base class method some_method(const QString) - which I don't, as the  
compiler tells me:

src# make
compiling test.cpp
test.cpp: In function ‘int main()’:
test.cpp:7: error: no matching function for call to  
‘xx::some_method(QString)’
test.h:12: note: candidates are: virtual void xx::some_method(int)
test.cpp: At global scope:
test.cpp:13: warning: unused parameter ‘i’
test.cpp:14: warning: unused parameter ‘i’
make: *** [test.o] Error 1

If I store the xx object in an x pointer (i.e. baseclass pointer), it  
works, but I lose access to any additional functionality of my derived  
class xx.

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

Thanks for your help!

Shimaron





More information about the Qt-interest-old mailing list