Function Hiding and Probable Reason behind it

Mridula's Avatar author of Function Hiding and Probable Reason behind it
This is an article on Function Hiding and Probable Reason behind it in C++.
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



Code: Cpp
#include <iostream>
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;
}
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Nominate this article for Article of the month for February 2009
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Vote for this article for Article of the month February 2009
Scripting's Avatar, Join Date: Jun 2010
John Hoder
This is literally the art of C language! I love it!