R
Ryan H.
A very simple example of something that acts quite odd:
class TopClass
{
public:
TopClass(){ }
virtual ~TopClass(){ }
virtual int myFunc() = 0;
};
class MiddleClass : public TopClass
{
public:
MiddleClass() : TopClass(){ }
virtual ~MiddleClass(){ }
virtual int myFunc( int x ) { return TopClass::myFunc()+x; }
};
class BottomClass : public MiddleClass
{
public:
BottomClass() : MiddleClass(){ }
virtual ~BottomClass(){ }
virtual int myFunc() { return 5; }
};
int
main( int argc, char* argv[] )
{
BottomClass c1;
c1.myFunc( 4 ); //will not compile: "'BottomClass::myFunc' : function
does not take 1 arguments"
return 0;
}
It seems that an instantiation of BottomClass cannot "see" the version
of myFunc that takes an int, declared in MiddleClass. However, both are
public and virtual, so why not?
I'm sure there is some very subtle reason why (probably having to do
with virtual function naming and overloading), but if anyone could
shine some light on this, we'd appreciate it. Some very experienced C++
guys I know have looked at it, without much luck.
Thanks all!
class TopClass
{
public:
TopClass(){ }
virtual ~TopClass(){ }
virtual int myFunc() = 0;
};
class MiddleClass : public TopClass
{
public:
MiddleClass() : TopClass(){ }
virtual ~MiddleClass(){ }
virtual int myFunc( int x ) { return TopClass::myFunc()+x; }
};
class BottomClass : public MiddleClass
{
public:
BottomClass() : MiddleClass(){ }
virtual ~BottomClass(){ }
virtual int myFunc() { return 5; }
};
int
main( int argc, char* argv[] )
{
BottomClass c1;
c1.myFunc( 4 ); //will not compile: "'BottomClass::myFunc' : function
does not take 1 arguments"
return 0;
}
It seems that an instantiation of BottomClass cannot "see" the version
of myFunc that takes an int, declared in MiddleClass. However, both are
public and virtual, so why not?
I'm sure there is some very subtle reason why (probably having to do
with virtual function naming and overloading), but if anyone could
shine some light on this, we'd appreciate it. Some very experienced C++
guys I know have looked at it, without much luck.
Thanks all!