views:

572

answers:

3

I wrote the code below in order to explain my issue. If I comment the line 11 (with the keyword "using"), the compiler does not compile the file and display this error: invalid conversion from 'char' to 'const char*'. It seems to do not see the method void action(char) of Parent class in Son class.
Why the compiler behaves this way? Or have I done something wrong?

class Parent
{
    public:
     virtual void action( const char how ){ this->action( &how ); }
     virtual void action( const char * how ) = 0;
};

class Son : public Parent
{
    public:
     using Parent::action; // Why shall i write this line?
     void action( const char * how ){ printf( "Action: %c\n", *how ); }
};

int main( int argc, char** argv )
{
    Son s = Son();
    s.action( 'a' );
    return 0;
}
+6  A: 

Surprisingly this is standard behavior. If a derived class declares a method with the same name as a method defined by the base class, the derived class' method hides the base class' one.

See http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9

Amnon
+10  A: 

The action declared in the derived class hides the action declared in the base class. If you use action on a Son object the compiler will search in the methods declared in Son, find one called action, and use that. It won't go on to search in the base class's methods, since it already found a matching name.

Then that method doesn't match the parameters of the call and you get an error.

See also the C++ FAQ Lite for more explanations on this topic.

sth
Don't you mean "The action declared in the base class *is hidden by*..." ?
Nate Kohl
@Nate: You're right, my words got somehow mixed up there when I made some changes before posting. Fixed it now.
sth