Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does implementation of abstract class not see overloaded pure virtual function?

Given the following code example, why is the overloaded AbstractBaseClass::DoAThing( const char* ) not visible as an inherited method in the SomeEndClass that implements the overloaded pure abstract DoAThing( const char* const* ) method?

class AbstractBaseClass
{
    public:

        virtual void DoAThing( const char* withThis ) {}
        virtual void DoAThing( const char* const* withThat ) = 0;

        AbstractBaseClass() {}
        virtual ~AbstractBaseClass() {}
};

class SomeMiddlewareClass : public AbstractBaseClass
{
    public:

        void ThisIsCool() {}

        SomeMiddlewareClass() {}
        virtual ~SomeMiddlewareClass() {}
};

class SomeEndClass : public SomeMiddlewareClass
{
    public:

        void DoAThing( const char* const* withThat ) {}

        SomeEndClass() {}
        virtual ~SomeEndClass() {}
};

void SomeFunction()
{
    SomeEndClass* myClass = new SomeEndClass();

    myClass->DoAThing( "withThis" );
    ((SomeMiddlewareClass*)myClass)->DoAThing( "withThisToo" );

    delete myClass;
}

The compiler (and indexer) produce the following error on the myClass->DoAThing( "withThis" ); line while the ((SomeMiddlewareClass*)myClass)->DoAThing( "withThisToo" ); line is accepted.

Invalid arguments ' Candidates are: void DoAThing(const char * const *)

no matching function for call to ‘SomeEndClass::DoAThing(const char [9])’

Shouldn't the SomeEndClass inherit the AbstractBaseClass::DoAThing( const char* ) implementation? What am I doing wrong?

like image 809
K Scott Piel Avatar asked Dec 12 '22 15:12

K Scott Piel


1 Answers

Your SomeEndClass::DoAThing() function not only overrides the function inherited from the base class, but also hides the other overloads of that function in the base class.

You could add a using declaration to your SomeEndClass class:

using SomeMiddlewareClass::DoAThing;

Therefore:

class SomeEndClass : public SomeMiddlewareClass
{
    public:

        using SomeMiddlewareClass::DoAThing;
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

        void DoAThing( const char* const* withThat ) {}

        SomeEndClass() {}
        virtual ~SomeEndClass() {}
};

With this fix, you can see your program compiling in this live example.

like image 73
Andy Prowl Avatar answered Feb 15 '23 22:02

Andy Prowl