Problem with Overriding an overloaded function

D

David Chandler

Let me try to describe the situation as clearly as I can.

In namespace XXX I have a class PARENT with the following public
functions:

virtual void foo( const char* stringBuf ); // set function for foo
virtual char* foo() const; // get function for foo

both of these functions have implementations in the PARENT.cpp file.

In namespace YYY I have a class CHILD

class CHILD: public XXX::pARENT
{
void foo( const char* stringBuff); // overrides parent's SET foo
// note that parent's GET foo is NOT overridden
}

CHILD.cpp has it's own foo implementation.

Along comes some class USER that uses CHILD.
YYY::CHILD* childPtr;
// some stuff that sets childPtr
someOtherString = childPtr->foo(); // should call the parent's GET

This line dies in the compiler.

"No matching function call to 'YYY::CHILD::foo() const'
Candidates are: 'void YYY::CHILD::foo(const char *)'

Any idea why this fails to find the parent's inherited foo function?

By the way, there ARE other child classes, lets call them CHILD2 that
do NOT override either of the parent's foo functions. These classes
are used without difficulty.

Thanks for your help

Dave
 
L

lilburne

David said:
Any idea why this fails to find the parent's inherited foo function?

If you redefine a base classes function fred then you hide all other
functions with different signatures called fred in the base class.
 
P

Phlip

David said:
In namespace XXX I have a class PARENT with the following public
functions:

virtual void foo( const char* stringBuf ); // set function for foo
virtual char* foo() const; // get function for foo

Those comments needed because the functions are poorly named. Overloading
get/set functions is cute, but it causes cognitive dissonance. Follow the
rule "name all functions with verb phrases".

Also, why is foo() returning a non-constant pointer? Users of foo() should
just tell PARENT what high-level thing they want to do to foo's data, and
let PARENT handle the details.
both of these functions have implementations in the PARENT.cpp file.

In namespace YYY I have a class CHILD

class CHILD: public XXX::pARENT
{
void foo( const char* stringBuff); // overrides parent's SET foo
// note that parent's GET foo is NOT overridden
}

CHILD.cpp has it's own foo implementation.

Along comes some class USER that uses CHILD.
YYY::CHILD* childPtr;
// some stuff that sets childPtr
someOtherString = childPtr->foo(); // should call the parent's GET

This line dies in the compiler.

"No matching function call to 'YYY::CHILD::foo() const'
Candidates are: 'void YYY::CHILD::foo(const char *)'

Any idea why this fails to find the parent's inherited foo function?

Because void YYY::CHILD::foo(const char *) does not "overload" char *
XXX::pARENT::foo(), it hides it. "Overload" means "same name within the same
scope". The lookup mechanics can only select overloads from one scope. So if
the compiler finds a near-match in one closer scope, it cannot look beyond
that scope to other scopes to find potential overloads.

Add "get" and "set" to your method names. Then try to take them out.
 
D

David Harmon

"No matching function call to 'YYY::CHILD::foo() const'
Candidates are: 'void YYY::CHILD::foo(const char *)'

Any idea why this fails to find the parent's inherited foo function?

It seems the error messages from recent compilers are less informative
here than some older compilers. Or, maybe you are not compiling with
the maximum warning level turned on?

Any function in the child class "hides" all functions of the same name
in the parent unless you bring them in with e.g.
using parent::foo;

For further discussion, See the topic "[23.6] What's the meaning of,
Warning: Derived::f(float) hides Base::f(int)?" in Marshall Cline's C++
FAQ, but discount Cline's alarmism on that subject. It is always good
to check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top