Why should I use the "using" keyword to access my base class method?

Viewed 45970

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

Why the compiler behave 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 should 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;
}
5 Answers

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 for more explanations on this topic.

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 C++ FAQ

By using using, the names declared in base class are introduced into the namespace of derived class.

Then, when you declare the set of functions in the derived class, they are distinguished from those with identical parameter types in its base class by compilers by the type of the implicit this pointer.

During overload resolution, an argument that needs a class-type conversion, which will happen when a pointer to derived class is converted to a pointer to a base class, is of the lowest priority.

So the two functions with seemingly identical parameter list can be distinguished, and when calling them from an object of the derived type, the one locally declared will be a better (if not exact) match.

I am a newbie and please point out my misunderstanding.

Related