This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
No overloading of virtual methods allowed within inheritance
- From: "Stuart Reynolds" <S dot Reynolds at mindlathe dot com>
- To: <gcc-bugs at gcc dot gnu dot org>
- Date: Thu, 7 Feb 2002 11:39:47 -0000
- Subject: No overloading of virtual methods allowed within inheritance
- Organization: Mindlathe
There seems to be a problem overloading
virtual method names in class hierarchies.
I'm fairly sure this is a compiler problem -
not an oversight/feature in the language
specification (but I could be wrong).
g++ v2.95.4, on debian Linux (kernel v2.2.19).
-------------
#include <iostream>
using namespace std;
class Output;
class S
{
public:
S() {}
virtual ~S() {}
virtual void write(ostream& os)
{
cout << "(default text)";
}
virtual void write(Output& out)=0;
};
class T : public S
{
public:
T() : S()
{}
virtual void write(Output& out)
{
}
/*
void write(X& x) is implemented here
void write(ostream& os) implemented in S
Class is concrete
*/
};
int main()
{
S* s = new T(); file://OK
s->write(cout);
T* t = new T();
t->write(cout); // Error
/**********
But this MUST work since every T and an S.
T::write(ostream& os)
is implemented as
S::write(ostream& os)
!
********/
return 0;
}
-------------
Remove
virtual void write(Output& out)=0;
and
virtual void write(Output& out)
{
}
and the program compiles - no worries.
The problem must be with
resolving the the overloaded name where
it is called since the class definitions
are parsed without incident.
Visual C++ 6 also falls over with this example.
Stuart