overloading base class functions

Discussion in 'C++' started by Rahul, Dec 18, 2007.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I read in an article that over loading a base class function
    (ordinary, non-static non-virtual member function) hides the base
    class versions in the derived class.

    However, as in the following code, i'm able to access the base class
    member functions,

    class A
    {
    public: void sample()
    {
    printf("A\n");
    }
    };

    class B : public A
    {
    public: void sample(int)
    {
    //A::sample();
    printf("B\n");
    }
    };

    int main()
    {
    B obj;
    obj.sample(5);
    obj.A::sample(); // Able to access base class versions...
    return(0);
    }

    What is correct significance for overloading?
     
    Rahul, Dec 18, 2007
    #1
    1. Advertising

  2. On Dec 18, 11:03 pm, Rahul <> wrote:
    > Hi Everyone,
    >
    > I read in an article that over loading a base class function
    > (ordinary, non-static non-virtual member function) hides the base
    > class versions in the derived class.
    >
    > However, as in the following code, i'm able to access the base class
    > member functions,
    >
    > class A
    > {
    > public: void sample()
    > {
    > printf("A\n");
    > }
    >
    > };
    >
    > class B : public A
    > {
    > public: void sample(int)
    > {
    > //A::sample();
    > printf("B\n");
    > }
    >
    > };
    >
    > int main()
    > {
    > B obj;
    > obj.sample(5);
    > obj.A::sample(); // Able to access base class versions...


    Yes, you can invoke the base member that way. But hiding is referred
    to as in you cannot get this to compile:

    obj.sample();

    (you can use using declaration in derived class to get this to work
    even)


    > What is correct significance for overloading?


    It is not called overloading. Overloading happens in same scope. This
    is re-defining (which causes hiding - I rhymed... yea!)
     
    Abhishek Padmanabh, Dec 18, 2007
    #2
    1. Advertising

  3. Rahul

    Pete Becker Guest

    On 2007-12-18 13:03:17 -0500, Rahul <> said:

    > I read in an article that over loading a base class function
    > (ordinary, non-static non-virtual member function) hides the base
    > class versions in the derived class.
    >
    > However, as in the following code, i'm able to access the base class
    > member functions,
    >


    That's because "hides" has no technical meaning, so you're left to
    guess at what it was intended to mean. The actual rule is that
    overloading occurs among signatures with the same name, defined in the
    same scope. When you define a name in a derived class, any members of
    the base class with the same name do not take part in overloading in
    the derived class. You can still see them, as your example shows, so
    they're not hiding. They're just not part of the derived class's
    overload set.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Dec 18, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,610
    Victor Bazarov
    Jun 21, 2004
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    549
    John Carson
    Sep 3, 2005
  3. Hicham Mouline
    Replies:
    1
    Views:
    596
    Victor Bazarov
    Apr 20, 2009
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    145
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page