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

phil prentice philp.cheer at talktalk.net
Thu May 7 11:24:51 CEST 2009


Hi
  I dont know if I am misunderstanding the problem.
  However I am not a C++ expert and am working hard to understand QT.
  I have read the following statement in the online version of 
"An Introduction to design patterens in C++ with QT"

 A member function of a derived class with the same name as a function in the 
base class hides all functions in the base class with that name. In such a 
case.
    * Only the derived class function can be called directly.
    *The class scope resolution operator may be used to call hidden base 
functions explicitly. 

Please see
  
http://cartan.cas.suffolk.edu/oopdocbook/opensource/overloadhide.html

I hope I have'nt misunderstood anything.

Phil
On Thursday 07 May 2009 06:25, Marco Borm wrote:
> 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
>
> _______________________________________________
> 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