Casting member function pointer

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

    Casting member function pointer


    Hi all,

    Given this situation:

    class Base
    {
    typedef void (Base::*FN_FOO) ();

    virtual void Foo() = 0; // pure virtual
    void GetFoo(FN_FOO pfnFoo) = 0; // pure virtual
    }

    class Derived : public Base
    {
    void Foo(); // implemented
    void GetFoo(FN_FOO pfnFoo); // implemented
    }

    Then, somewhere in the code:

    Derived derInstance;
    FN_FOO pfnFoo;
    derInstance.Get Foo(pfnFoo);

    GetFoo is implemented like this:

    void Derived::GetFoo (FN_FOO pfnFoo)
    {
    pfnFoo = &derInstance::F oo;
    }

    I get a compiler error that says cannot convert from Derived::* to
    GraphicsEngine: :*, conversion requires reinterpret_cas t, C-style cast
    or function-style cast.

    Can anyone help me with the syntax for this cast?

    Thanks!




    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----
  • Bren

    #2
    Re: Casting member function pointer

    On Thu, 18 Sep 2003 11:26:18 -0400, Bren <iambrenNOSPAM@ sympatico.ca>
    wrote:
    [color=blue]
    >I get a compiler error that says cannot convert from Derived::* to
    >GraphicsEngine ::*, conversion requires reinterpret_cas t, C-style cast
    >or function-style cast.[/color]

    Duh, sorry, that SHOULD read:

    I get a compiler error that says cannot convert from Derived::* to
    Base::*, conversion requires reinterpret_cas t, C-style cast or
    function-style cast.

    I was generalizing the actual names. :)

    Thanks!



    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

    Comment

    • Bren

      #3
      Re: Casting member function pointer

      On Thu, 18 Sep 2003 11:32:44 -0400, Bren <iambrenNOSPAM@ sympatico.ca>
      wrote:
      [color=blue]
      >I get a compiler error that says cannot convert from Derived::* to
      >Base::*, conversion requires reinterpret_cas t, C-style cast or
      >function-style cast.[/color]

      Got it!

      pfnFoo = = (void(Base::*)( ))&Derived::Fo o



      -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
      http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
      -----== Over 100,000 Newsgroups - 19 Different Servers! =-----

      Comment

      • Gianni Mariani

        #4
        Re: Casting member function pointer

        Bren wrote:[color=blue]
        > On Thu, 18 Sep 2003 11:32:44 -0400, Bren <iambrenNOSPAM@ sympatico.ca>
        > wrote:
        >
        >[color=green]
        >>I get a compiler error that says cannot convert from Derived::* to
        >>Base::*, conversion requires reinterpret_cas t, C-style cast or
        >>function-style cast.[/color]
        >
        >
        > Got it!
        >
        > pfnFoo = = (void(Base::*)( ))&Derived::Fo o
        >[/color]

        I'm not sure this does what you think it does.

        It might work for you but it's certainly questionable if it's correct at
        all.

        What are you trying to do ?



        Comment

        • Ron Natalie

          #5
          Re: Casting member function pointer


          "Bren" <iambrenNOSPAM@ sympatico.ca> wrote in message news:8qijmvc1mt e46ilr37v0pjgrf q1icr0fk8@4ax.c om...
          [color=blue]
          > void Derived::GetFoo (FN_FOO pfnFoo)
          > {
          > pfnFoo = &derInstance::F oo;
          > }
          >
          > I get a compiler error that says cannot convert from Derived::* to
          > GraphicsEngine: :*, conversion requires reinterpret_cas t, C-style cast
          > or function-style cast.
          >
          > Can anyone help me with the syntax for this cast?[/color]

          You use a reinterpret cast, C-syle cast, or function cast.

          pfnFoo = reinterpret_cas t<FN_FOO>(&Deri ved::Foo);

          You have a number of misconceptions and other problems as well.

          1. You must form the pointer to member with the qulaified name (i.e.
          Derived::Foo) rather than what you wrote which is ill-formed. You
          also forgot the virtual key on the function GetFoo and a lot of semicolons.

          2. There's no implicit Derived member pointer to Base member pointer.
          Think of it this way: Every member of Base is a member of Derived
          but not vice versa. The cast is potenitially unsafe.

          3. I don't understand why you are screwing with the derived class member
          at all. Since Foo is a virtual function in the base class, you could have
          just written:
          ptnFoo = &Base::Foo;
          The virtual invocation gets applied after the member pointer is evaluated
          so this will call Derived::Foo (for objects of type Derived) anyhow.


          Comment

          Working...