calling virtual function that is hidden by inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kris Thielemans

    calling virtual function that is hidden by inheritance

    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
  • Mike Wahler

    #2
    Re: calling virtual function that is hidden by inheritance


    "Kris Thielemans" <kris.thieleman s@imperial.ac.u k> wrote in message
    news:6c1545b0.0 310071556.50a3e faf@posting.goo gle.com...[color=blue]
    > 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.[/color]

    #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


    Comment

    • Ali R.

      #3
      Re: calling virtual function that is hidden by inheritance

      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" <kris.thieleman s@imperial.ac.u k> wrote in message
      news:6c1545b0.0 310071556.50a3e faf@posting.goo gle.com...[color=blue]
      > 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[/color]


      Comment

      • Rob Williscroft

        #4
        Re: calling virtual function that is hidden by inheritance

        Kris Thielemans wrote in
        news:6c1545b0.0 310071556.50a3e faf@posting.goo gle.com:
        [color=blue]
        >
        > void some_routine()
        > { B b;
        > b.func(); // will call B::func
        > ??? // how to call A::func() for the b object[/color]

        b.A::func();
        [color=blue]
        > }
        >[/color]

        HTH

        Rob.
        --

        Comment

        • tom_usenet

          #5
          Re: calling virtual function that is hidden by inheritance

          On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
          <mkwahler@mkwah ler.net> wrote:
          [color=blue]
          >int main()
          >{
          > B b;
          > b.func(); /* prints "B" */
          > static_cast<A>( b).func(); /* prints "A" */[/color]

          Did you mean to copy the object?

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

          Tom

          Comment

          • Kris Thielemans

            #6
            Re: calling virtual function that is hidden by inheritance

            "Mike Wahler" <mkwahler@mkwah ler.net> wrote in message news:<WOIgb.175 7$av5.774@newsr ead3.news.pas.e arthlink.net>.. .[color=blue]
            > "Kris Thielemans" <kris.thieleman s@imperial.ac.u k> wrote in message
            > news:6c1545b0.0 310071556.50a3e faf@posting.goo gle.com...[/color]

            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
            [color=blue]
            > 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[/color]

            Comment

            • Ron Natalie

              #7
              Re: calling virtual function that is hidden by inheritance


              "Kris Thielemans" <kris.thieleman s@imperial.ac.u k> wrote in message news:6c1545b0.0 310071556.50a3e faf@posting.goo gle.com...
              [color=blue]
              > 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
              > }
              >[/color]

              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.


              Comment

              • Kris Thielemans

                #8
                Re: calling virtual function that is hidden by inheritance

                "Ron Natalie" <ron@sensor.com > wrote in message news:<3f84281b$ 0$37005$9a6e19e a@news.newshost ing.com>...[color=blue]
                > "Kris Thielemans" <kris.thieleman s@imperial.ac.u k> wrote in message news:6c1545b0.0 310071556.50a3e faf@posting.goo gle.com...
                >[color=green]
                > > class A { virtual void func(); };
                > > class B: public A { virtual void func(); }[/color]
                >
                > 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.[/color]

                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

                Comment

                • Kris Thielemans

                  #9
                  Re: calling virtual function that is hidden by inheritance

                  tom_usenet <tom_usenet@hot mail.com> wrote in message news:<bck7ov8mh 8dskvulkpp5lqko i7ogi6ki4l@4ax. com>...[color=blue]
                  > On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
                  > <mkwahler@mkwah ler.net> wrote:
                  >
                  > static_cast<A&> (b).func();[/color]

                  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



                  [color=blue]
                  > or just
                  > b.A::func();
                  >[/color]

                  this works fine.

                  Kris

                  Comment

                  • tom_usenet

                    #10
                    Re: calling virtual function that is hidden by inheritance

                    On 9 Oct 2003 06:44:06 -0700, kris.thielemans @imperial.ac.uk (Kris
                    Thielemans) wrote:
                    [color=blue]
                    >tom_usenet <tom_usenet@hot mail.com> wrote in message news:<bck7ov8mh 8dskvulkpp5lqko i7ogi6ki4l@4ax. com>...[color=green]
                    >> On Wed, 08 Oct 2003 00:24:22 GMT, "Mike Wahler"
                    >> <mkwahler@mkwah ler.net> wrote:
                    >>
                    >> static_cast<A&> (b).func();[/color]
                    >
                    >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().[/color]

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

                    Tom

                    Comment

                    Working...