Function Hiding and Probable Reason behind it

Discussion in 'C++' started by Mridula, Feb 26, 2009.

  1. Mridula

    Mridula New Member

    Joined:
    Mar 5, 2008
    Messages:
    316
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    S/w proffessional
    Location:
    Bangalore
    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:
     
    #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;
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  4. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    This is literally the art of C language! I love it!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice