How can I get the address of a virtual member function?

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

    How can I get the address of a virtual member function?

    Hello,

    I need to pass a pointer to a
    callback function to the lower
    level modules. But the function
    is thought to be a virtual member one.

    How can I get the real address of
    the virtual member function?

    Best regards,
    Roy


  • Jerry Coffin

    #2
    Re: How can I get the address of a virtual member function?

    In article <bl5cmu$1e5t$1@ mail.cn99.com>, flyao@263.net says...[color=blue]
    > Hello,
    >
    > I need to pass a pointer to a
    > callback function to the lower
    > level modules. But the function
    > is thought to be a virtual member one.
    >
    > How can I get the real address of
    > the virtual member function?[/color]

    The usual way to handle this depends on being able to pass a parameter
    that will be passed back to the callback function when it gets called.
    Assuming that's so (and it usually is) you typically use a static member
    function as the callback function, and pass the address of the class as
    the parameter:

    class base {

    virtual void callback() = 0;

    static real_callback(v oid *v) {
    static_cast<bas e *>(v)->callback();
    }
    };

    class derived : public base {

    virtual void callback() {
    // whatever
    }

    OS_call_that_ca lls_back(static _cast<void *>(this));
    };

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.

    Comment

    Working...