Why doesn't the abstract class implementation see an overloaded pure virtual function?

Given the following code example, why does the overloaded AbstractBaseClass::DoAThing( const char* ) not display as an inherited method in 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) creates the following error in the line myClass->DoAThing( "withThis" ); , while the string ((SomeMiddlewareClass*)myClass)->DoAThing( "withThisToo" ); accepted.

Invalid arguments. Candidates: void Doathing (const char * const *)

there is no corresponding function to call "SomeEndClass :: Doathing (const char [9])

Should SomeEndClass inherit the implementation of AbstractBaseClass::DoAThing( const char* ) ? What am I doing wrong?

+4
source share
2 answers

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

You can add a using declaration to your SomeEndClass class:

 using SomeMiddlewareClass::DoAThing; 

In this way:

 class SomeEndClass : public SomeMiddlewareClass { public: using SomeMiddlewareClass::DoAThing; // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ void DoAThing( const char* const* withThat ) {} SomeEndClass() {} virtual ~SomeEndClass() {} }; 

With this fix, you can see the compilation of your program in this live example .

+7
source

In the base class, DoAThing works not only virtual, but also overloaded.

A function in a derived class overrides one of these overloads and hides the other.

Then you try to call another one that is hidden.

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

 using Base::DoAThing; 

... but do you need a separate (more complex) question.

+6
source

All Articles