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

Marco Borm qt-lists at retrodesignfan.eu
Thu May 7 08:25:18 CEST 2009


Hi Shimaron,

I think your code is complete ok.
I had the same problem many times and think and the reason for that was 
the buggy MS compiler I used, not that this code was invalid. The GCC 
just worked in that cases as I expected.
What compiler are you using?


Greetings,
Marco

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:
>
> ===== 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
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>   




More information about the Qt-interest-old mailing list