Calling identically-named parent method

Discussion in 'C++' started by Spoon, Dec 20, 2007.

  1. Spoon

    Spoon Guest

    Hello,

    Consider the following code.

    $ cat foo.C
    #include <cstdio>

    struct A
    {
    int bar() { puts("A::bar()"); return 666; }
    int Abar() { puts("A::bar()"); return 666; }
    };

    struct B : public A
    {
    void bar(int n) { printf("B::bar(%d)\n", n); }
    };

    int main()
    {
    B foo;
    foo.bar(123);
    foo.bar();
    foo.A::bar();
    foo.Abar();
    return 0;
    }

    Both A and B provide a method 'bar' but the signature is different.
    int bar(void) in A
    void var(int) in B

    I have an object of type B, and I want to call A's bar.

    The ovious foo.bar() is considered a syntax error by GCC.

    $ g++ -Wall -Wextra -std=c++98 -pedantic foo.C
    foo.C: In function `int main()':
    foo.C:18: error: no matching function for call to `B::bar()'
    foo.C:11: note: candidates are: void B::bar(int)

    Does this mean I either have to change the name of A's bar as in Abar,
    or qualify bar as in A::bar? Are there other alternatives?

    Regards.
     
    Spoon, Dec 20, 2007
    #1
    1. Advertising

  2. On Dec 20, 1:42 pm, Spoon <root@localhost> wrote:
    > Does this mean I either have to change the name of A's bar as in Abar,
    > or qualify bar as in A::bar? Are there other alternatives?


    struct B : public A
    {
    using A::bar;
    void bar(int n) { printf("B::bar(%d)\n", n); }

    };

    Check out name hiding. http://www.parashift.com/c -faq-lite/strange-inheritance.html#faq-23.9
     
    Ioannis Gyftos, Dec 20, 2007
    #2
    1. Advertising

  3. Spoon

    Rahul Guest

    On Dec 20, 4:42 pm, Spoon <root@localhost> wrote:
    > Hello,
    >
    > Consider the following code.
    >
    > $ cat foo.C
    > #include <cstdio>
    >
    > struct A
    > {
    > int bar() { puts("A::bar()"); return 666; }
    > int Abar() { puts("A::bar()"); return 666; }
    >
    > };
    >
    > struct B : public A
    > {
    > void bar(int n) { printf("B::bar(%d)\n", n); }
    >
    > };
    >
    > int main()
    > {
    > B foo;
    > foo.bar(123);
    > foo.bar();
    > foo.A::bar();
    > foo.Abar();
    > return 0;
    >
    > }
    >
    > Both A and B provide a method 'bar' but the signature is different.
    > int bar(void) in A
    > void var(int) in B
    >
    > I have an object of type B, and I want to call A's bar.
    >
    > The ovious foo.bar() is considered a syntax error by GCC.
    >
    > $ g++ -Wall -Wextra -std=c++98 -pedantic foo.C
    > foo.C: In function `int main()':
    > foo.C:18: error: no matching function for call to `B::bar()'
    > foo.C:11: note: candidates are: void B::bar(int)
    >
    > Does this mean I either have to change the name of A's bar as in Abar,
    > or qualify bar as in A::bar? Are there other alternatives?
    >
    > Regards.


    Redefining base class member function in the derived class, hides all
    the implementation of the base class members. You could use,

    foo.A::bar();
     
    Rahul, Dec 20, 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.  opalinski from opalpaweb
    Replies:
    9
    Views:
    2,644
    opalinski from opalpaweb
    Mar 1, 2006
  2. Stanimir Stamenkov
    Replies:
    9
    Views:
    1,034
    David Carlisle
    Jan 26, 2005
  3. Joris Gillis
    Replies:
    9
    Views:
    413
    Jan Roland Eriksson
    Jan 8, 2005
  4. Simon Strandgaard

    compare two identically hashes fails

    Simon Strandgaard, Oct 6, 2003, in forum: Ruby
    Replies:
    6
    Views:
    152
    Kent Dahl
    Oct 6, 2003
  5. Tore Aursand

    OOP: Inheriting identically named methods

    Tore Aursand, Feb 17, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    116
    Tore Aursand
    Feb 17, 2004
Loading...

Share This Page