Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Function Hiding and Probable Reason behind it (http://www.go4expert.com/articles/function-hiding-probable-reason-t16352/)

Mridula 26Feb2009 19:04

Function Hiding and Probable Reason behind it
 
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 4Mar2009 09:56

Re: Function Hiding and Probable Reason behind it
 
Nominate this article for Article of the month for February 2009

shabbir 17Mar2009 12:17

Re: Function Hiding and Probable Reason behind it
 
Vote for this article for Article of the month February 2009

Scripting 20Jul2012 01:41

Re: Function Hiding and Probable Reason behind it
 
This is literally the art of C language! I love it!


All times are GMT +5.5. The time now is 19:22.