inheritance and name resolution

Discussion in 'C++' started by Srini, Aug 16, 2005.

  1. Srini

    Srini Guest

    Hello,

    I've had this long lasting doubt. When a sub-class re-defines a
    function that's in its superclass, it hides the superclass function.

    class A {
    public:
    void foo(int) { }
    };

    class B : public A {
    public:
    void foo(std::string) { }
    };

    B obj;
    obj.foo(10); // Error, no matching function B::foo(int)

    Here B::foo(std::string) hides A::foo(int). So, if I want to bring
    A::foo(int) into visibility, I've to put a using directive.

    class B : public A {
    public:
    using A::foo; // bring A::foo(int) into scope here
    void foo(std::string) { }
    };

    Now the normal overload function call resolution would happen. In that
    case, why is the following not an error?

    class A {
    public:
    void foo(int) { }
    };

    class B : public A {
    public:
    using A::foo;
    void foo(int) { }
    };

    B obj;
    obj.foo(10); // This would call B::foo(int)

    Should this not result in a compile error because there are 2 functions
    with the same signature?

    Thanks,
    Srini
    Srini, Aug 16, 2005
    #1
    1. Advertising

  2. * Srini:
    > why is the following not an error?
    >
    > class A {
    > public:
    > void foo(int) { }
    > };
    >
    > class B : public A {
    > public:
    > using A::foo;
    > void foo(int) { }
    > };
    >
    > B obj;
    > obj.foo(10); // This would call B::foo(int)


    ยง7.3.3/12 explicitly allows this, stating that B::foo hides A::foo (the
    example given is essentially the same as yours).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 16, 2005
    #2
    1. Advertising

  3. Srini

    benben Guest

    "Srini" <> wrote in message
    news:...
    > Hello,
    >
    > I've had this long lasting doubt. When a sub-class re-defines a
    > function that's in its superclass, it hides the superclass function.
    >
    > class A {
    > public:
    > void foo(int) { }
    > };
    >
    > class B : public A {
    > public:
    > void foo(std::string) { }
    > };
    >
    > B obj;
    > obj.foo(10); // Error, no matching function B::foo(int)
    >
    > Here B::foo(std::string) hides A::foo(int). So, if I want to bring
    > A::foo(int) into visibility, I've to put a using directive.
    >
    > class B : public A {
    > public:
    > using A::foo; // bring A::foo(int) into scope here
    > void foo(std::string) { }
    > };
    >
    > Now the normal overload function call resolution would happen. In that
    > case, why is the following not an error?
    >
    > class A {
    > public:
    > void foo(int) { }
    > };
    >
    > class B : public A {
    > public:
    > using A::foo;
    > void foo(int) { }
    > };
    >
    > B obj;
    > obj.foo(10); // This would call B::foo(int)
    >
    > Should this not result in a compile error because there are 2 functions
    > with the same signature?
    >
    > Thanks,
    > Srini
    >


    Consider:

    class base
    {
    public:
    void foo(int);
    void foo(double);
    void foo(std::string);
    };

    You are now require to override void base::foo(int), and also make the other
    two overloads visible, how would you do?

    class derived: public base
    {
    public:
    using base::foo;

    void foo(int);
    };

    Ben
    benben, Aug 16, 2005
    #3
  4. Srini

    Srini Guest

    Got it! A function in the derived class *always* hides the function
    with the same signature, in the base class. Thank you Alf and Ben.
    Srini, Aug 16, 2005
    #4
    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. Andrew FPGA
    Replies:
    0
    Views:
    961
    Andrew FPGA
    Sep 26, 2005
  2. Lars Both
    Replies:
    4
    Views:
    2,162
    Lars Both
    May 31, 2007
  3. rocky
    Replies:
    2
    Views:
    313
    rocky
    Jul 7, 2009
  4. Christopher

    inheritance - method resolution

    Christopher, Apr 7, 2011, in forum: C++
    Replies:
    5
    Views:
    229
    red floyd
    Apr 7, 2011
  5. Xavier Noria
    Replies:
    2
    Views:
    103
    Xavier Noria
    Nov 16, 2007
Loading...

Share This Page