Skip to content Skip to sidebar Skip to footer

Function Hiding and Probable Reason behind it


Introduction



Talks about what is function hiding and reason behind it. 

Background



In the example given below you would notice that the func() method has different signatures in the base and derived class. 
Thus its neither a case of function over-riding (which needs same function signature in base and derived class) nor of function over-loading (which needs same function name with different signature in the SAME class).

the base class func(int) would never be accessible using the derived's object. So the base class' func(int) would have got hidden since we are using a different signature of the same function name in derived class.

Reason could be:

In C++, when you declare a method in the derived class with the same *name* as the method in its some base class, it is always treated as overriding. That's why, even if your method was declared with different types of arguments, that method from the base class is "not derived" (that is, this metod will not become a method of derived class).



The code



#include
using namespace std;
struct Base
{
virtual void func( int ) {}
};

struct Derived: Base
{
void func( char const [] ) {}
};

int main()
{
    Derived d;
    d.func( 1234 );
    return 0;
}

Tell the World ...


by : go4expert