No overloading of virtual methods allowed within inheritance

Stuart Reynolds S.Reynolds@mindlathe.com
Thu Feb 7 04:11:00 GMT 2002


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









More information about the Gcc-bugs mailing list