What's in a Name: Overridden and Hidden

Dear Dr. GUI:

I am using Microsoft Visual C++ 4.0, and I am experiencing what to me seems like a strange problem.

class CBase

{

public:

virtual MyFunc( int );

virtual MyFunc( void );

};

 

class CDerived : public CBase

{

public:

virtual MyFunc( void );

};

As you can see, the base class has two virtual functions, the derived class overrides one of these virtual functions. This code compiles without a problem as expected.

Now we create an instance of the derived class as follows and use it.

CDerived a;

a.MyFunc( 10 );

The compiler does not like this. The error that is thrown out reads something like this:

error C2660: 'MyFunc' : function does not take 1 parameters.

This has been tried on both Visual C++ 4.0 and 4.1. I see what is happening, but I don't understand why shouldn't MyFunc(int) be inherited from the base class?

Chris Taylor

Dr. GUI replies:

This is because of the C++ rule of name-hiding (you can look in the C++ ARM under name-hiding). When a virtual function in a base class B is overloaded and then overridden in a derived class C, the override of the virtual function in the derived class C "hides" all overloads of the function name in the base class.