calling virtual function that is hidden by inheritance

Discussion in 'C++' started by Kris Thielemans, Oct 8, 2003.

  1. Hi

    I have a rather outlandish problem where I want to call a virtual
    function which is (sort of) hidden by a derived class. For instance

    class A { virtual void func(); };
    class B: public A { virtual void func(); }

    void some_routine()
    { B b;
    b.func(); // will call B::func
    ??? // how to call A::func() for the b object
    }

    I know how to do this within the definition of a member. For instance
    void B::func()
    {
    A::func();
    }

    but I need it outside the class.

    In case you wonder why I need it... I am writing a test program. In my
    case A::func() and B::func() should give the same results, but
    B::func() does it faster. My test program wants to check if the
    results are indeed the same.

    Thanks for any help!

    Kris
     
    Kris Thielemans, Oct 8, 2003
    #1
    1. Advertising

  2. Kris Thielemans

    Mike Wahler Guest

    "Kris Thielemans" <> wrote in message
    news:...
    > Hi
    >
    > I have a rather outlandish problem where I want to call a virtual
    > function which is (sort of) hidden by a derived class. For instance
    >
    > class A { virtual void func(); };
    > class B: public A { virtual void func(); }
    >
    > void some_routine()
    > { B b;
    > b.func(); // will call B::func
    > ??? // how to call A::func() for the b object
    > }
    >
    > I know how to do this within the definition of a member. For instance
    > void B::func()
    > {
    > A::func();
    > }
    >
    > but I need it outside the class.
    >
    > In case you wonder why I need it... I am writing a test program. In my
    > case A::func() and B::func() should give the same results, but
    > B::func() does it faster. My test program wants to check if the
    > results are indeed the same.


    #include <iostream>

    class A
    {
    public:
    virtual void func() { std::cout << "A\n"; }
    };

    class B: public A
    {
    public:
    virtual void func() { std::cout << "B\n"; }
    };

    int main()
    {
    B b;
    b.func(); /* prints "B" */
    static_cast<A>(b).func(); /* prints "A" */
    return 0;
    }

    -Mike
     
    Mike Wahler, Oct 8, 2003
    #2
    1. Advertising

  3. Kris Thielemans

    Ali R. Guest

    Hi Kris,

    Mike's way is one way. Fortunately this is more than one way to skin the
    cat. I like this one better :
    what you do is use the name of the parent class just like a member variable
    followed by ::

    class Parent
    {
    public:
    virtual void func() { std::cout << "A\n"; }
    };

    class Child : public Parent
    {
    public:
    virtual void func() { std::cout << "B\n"; }
    };

    int main()
    {
    Child b;

    b.func(); /* prints "B" */
    b.Parent::func(); /* prints "A" */

    return 0;
    }


    Ali R.

    "Kris Thielemans" <> wrote in message
    news:...
    > Hi
    >
    > I have a rather outlandish problem where I want to call a virtual
    > function which is (sort of) hidden by a derived class. For instance
    >
    > class A { virtual void func(); };
    > class B: public A { virtual void func(); }
    >
    > void some_routine()
    > { B b;
    > b.func(); // will call B::func
    > ??? // how to call A::func() for the b object
    > }
    >
    > I know how to do this within the definition of a member. For instance
    > void B::func()
    > {
    > A::func();
    > }
    >
    > but I need it outside the class.
    >
    > In case you wonder why I need it... I am writing a test program. In my
    > case A::func() and B::func() should give the same results, but
    > B::func() does it faster. My test program wants to check if the
    > results are indeed the same.
    >
    > Thanks for any help!
    >
    > Kris
     
    Ali R., Oct 8, 2003
    #3
  4. Kris Thielemans wrote in
    news::

    >
    > void some_routine()
    > { B b;
    > b.func(); // will call B::func
    > ??? // how to call A::func() for the b object


    b.A::func();

    > }
    >


    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Oct 8, 2003
    #4
  5. Kris Thielemans

    tom_usenet Guest

    On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
    <> wrote:

    >int main()
    >{
    > B b;
    > b.func(); /* prints "B" */
    > static_cast<A>(b).func(); /* prints "A" */


    Did you mean to copy the object?

    static_cast<A&>(b).func();
    or just
    b.A::func();

    Tom
     
    tom_usenet, Oct 8, 2003
    #5
  6. "Mike Wahler" <> wrote in message news:<WOIgb.1757$>...
    > "Kris Thielemans" <> wrote in message
    > news:...


    Thanks Mike

    however it seems that your solution doesn't work for me (using gcc
    3.2). The reason being that my base class A has other pure (and
    unimplemented) virtual members. When I do the static_cast<A> trick I
    get an error "cannot allocate an object of type A because the
    following virutal functions are abstract ..."

    In contrast, the suggestion by Ali and Rob (to use b.A::func()) works
    fine in that case as well.

    Thanks all 3 of you.

    Kris

    > class A
    > {
    > public:
    > virtual void func() { std::cout << "A\n"; }
    > };
    >
    > class B: public A
    > {
    > public:
    > virtual void func() { std::cout << "B\n"; }
    > };
    >
    > int main()
    > {
    > B b;
    > b.func(); /* prints "B" */
    > static_cast<A>(b).func(); /* prints "A" */
    > return 0;
    > }
    >
    > -Mike
     
    Kris Thielemans, Oct 8, 2003
    #6
  7. Kris Thielemans

    Ron Natalie Guest

    "Kris Thielemans" <> wrote in message news:...

    > class A { virtual void func(); };
    > class B: public A { virtual void func(); }
    >
    > void some_routine()
    > { B b;
    > b.func(); // will call B::func
    > ??? // how to call A::func() for the b object
    > }
    >


    You can't do this. That's the whole point of access control.
    You can't access A's private parts. It's got nothing whatsoever
    to do with virtual or hiding.
     
    Ron Natalie, Oct 8, 2003
    #7
  8. "Ron Natalie" <> wrote in message news:<3f84281b$0$37005$>...
    > "Kris Thielemans" <> wrote in message news:...
    >
    > > class A { virtual void func(); };
    > > class B: public A { virtual void func(); }

    >
    > You can't do this. That's the whole point of access control.
    > You can't access A's private parts. It's got nothing whatsoever
    > to do with virtual or hiding.


    oops. sorry. forgot the public there! You're right of course. but the
    other posters gave me the answer I wanted anyway. lucky me.

    thanks

    kris
     
    Kris Thielemans, Oct 9, 2003
    #8
  9. tom_usenet <> wrote in message news:<>...
    > On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
    > <> wrote:
    >
    > static_cast<A&>(b).func();


    right, that would solve my pure virtual problem I mentioned.

    However, this static_cast trick actually does not work. It will still
    call B::func(). I checked this with gcc, but I find the easiest
    explanation as follows:

    In my opinion, the above is functionally identical to

    A& a_ref = b; /* feel free to insert a static_cast here, but you
    don't have to*/
    a_ref.func();

    Now, I hope you'll agree that the last statement definitely calls
    B::func(). Otherwise there would be no point in having virtual
    functions. Indeed suppose you have a function that works on A&
    objects, which version of func() do you want it to call normally?

    void some_other_func(A& a)
    {
    a.func();
    }
    some_other_func(b); // will call B::func




    > or just
    > b.A::func();
    >


    this works fine.

    Kris
     
    Kris Thielemans, Oct 9, 2003
    #9
  10. Kris Thielemans

    tom_usenet Guest

    On 9 Oct 2003 06:44:06 -0700, (Kris
    Thielemans) wrote:

    >tom_usenet <> wrote in message news:<>...
    >> On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
    >> <> wrote:
    >>
    >> static_cast<A&>(b).func();

    >
    >right, that would solve my pure virtual problem I mentioned.
    >
    >However, this static_cast trick actually does not work. It will still
    >call B::func().


    Whoops, of course, since func is virtual (I hadn't spotted that).

    Tom
     
    tom_usenet, Oct 9, 2003
    #10
    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. cppsks
    Replies:
    0
    Views:
    832
    cppsks
    Oct 27, 2004
  2. Andreas Lagemann
    Replies:
    8
    Views:
    502
    Mike Wahler
    Jan 10, 2005
  3. tiwy
    Replies:
    0
    Views:
    453
  4. Ashwin
    Replies:
    2
    Views:
    356
    Pierre Barbier de Reuille
    Aug 1, 2006
  5. Replies:
    11
    Views:
    706
    James Kanze
    Sep 10, 2006
Loading...

Share This Page