Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

In your base class DoAThing is not just virtual, but overloaded.

The function in the derived class overrides one of those overloads and hides the other.

You're then trying to call the other, which is hidden.

You can make the hidden function visible in the derived class with a using declaration:

using Base::DoAThing;

...but whether you should is a separate (more complex) question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top