Casting a void pointer to base class and calling overridden function ... allowed?

Discussion in 'C++' started by Bj?rn Toft Madsen, Mar 1, 2004.

  1. Hi all,

    The network library I use communicates with my app using a callback
    function. This callback function is called with the type of message, a
    void pointer to he actual message and a user defined void pointer.

    The callback function expected is a standard C callback function. I
    have used a static class function, which I certainly know is not
    entirely correct. Bear with me, I don't think my troubles are related
    to this hack.

    When the class is constructed, I set up the network library by
    supplying the address of the static method as the callback and I also
    pass along "this" as the user defined void pointer.

    In the static method, the following takes place:
    HRESULT CDPServer::MessageHandler(PVOID pvUserContext, DWORD
    dwMessageType, PVOID pMessage) {
    switch(dwMessageType) {
    case DPN_MSGID_ADD_PLAYER_TO_GROUP:
    return ((CDPServer*)pvUserContext)->AddPlayerToGroup((PDPNMSG_ADD_PLAYER_TO_GROUP)pMessage);
    break;
    //many more cases
    }
    }

    As you can see, I cast the user defined pointer to CDPServer*.

    The issue is that naturally all these "event handlers" like
    AddPlayerToGroup() are meant to be overridden in sub-classes of
    CDPServer:
    class CMyServer : public CDPServer {
    protected:
    //other stuff
    virtual HRESULT AddPlayerToGroup(/*...*/);
    }

    So in effect, my static message handler casts the pointer to
    CMyServer's super class (CDPServer) and then calls the overridden
    function AddPlayerToGroup()

    The application behaves as expected when running, but my debugger (a
    rather old MSVC++ 6.0) jumps into CDPServer::AddPlayerToGroup()
    instead of CMyServer::AddPlayerToGroup(). Once in there, it goes about
    executing empty lines and generally behaves weird.

    Questions that I hope somebody cleverer than I can answer:
    1) Is this approach canon?
    2) Is the fact that the debugger jumps into
    CDPServer::AddPlayerToGroup() related to me using a bad approach, or
    can I chalk it up to my rather old debugger? When running, after all,
    things happen the way they are suppose to.

    My suggestions as to how these issues could be alleviated; please
    don't hesitate to comment:
    1) Use typeid to discern the correct type and thus make sure that I
    call the correct method. Isn't this what virtual does?
    2) Visitor pattern (supply a message handler object and then have
    CDPServer call functions in this) instead of deriving from CDPServer.

    Thanks in advance for any pointers you might have.
    Best regards
    Bjørn Toft Madsen
     
    Bj?rn Toft Madsen, Mar 1, 2004
    #1
    1. Advertising

  2. "Bj?rn Toft Madsen" <> wrote in message
    news:...
    > Hi all,
    >
    > The network library I use communicates with my app using a callback
    > function. This callback function is called with the type of message, a
    > void pointer to he actual message and a user defined void pointer.
    >
    > The callback function expected is a standard C callback function. I
    > have used a static class function, which I certainly know is not
    > entirely correct. Bear with me, I don't think my troubles are related
    > to this hack.
    >
    > When the class is constructed, I set up the network library by
    > supplying the address of the static method as the callback and I also
    > pass along "this" as the user defined void pointer.
    >
    > In the static method, the following takes place:
    > HRESULT CDPServer::MessageHandler(PVOID pvUserContext, DWORD
    > dwMessageType, PVOID pMessage) {
    > switch(dwMessageType) {
    > case DPN_MSGID_ADD_PLAYER_TO_GROUP:
    > return

    ((CDPServer*)pvUserContext)->AddPlayerToGroup((PDPNMSG_ADD_PLAYER_TO_GROUP)p
    Message);
    > break;
    > //many more cases
    > }
    > }
    >
    > As you can see, I cast the user defined pointer to CDPServer*.
    >
    > The issue is that naturally all these "event handlers" like
    > AddPlayerToGroup() are meant to be overridden in sub-classes of
    > CDPServer:
    > class CMyServer : public CDPServer {
    > protected:
    > //other stuff
    > virtual HRESULT AddPlayerToGroup(/*...*/);
    > }
    >
    > So in effect, my static message handler casts the pointer to
    > CMyServer's super class (CDPServer) and then calls the overridden
    > function AddPlayerToGroup()
    >
    > The application behaves as expected when running, but my debugger (a
    > rather old MSVC++ 6.0) jumps into CDPServer::AddPlayerToGroup()
    > instead of CMyServer::AddPlayerToGroup(). Once in there, it goes about
    > executing empty lines and generally behaves weird.
    >
    > Questions that I hope somebody cleverer than I can answer:
    > 1) Is this approach canon?


    I think its fine.

    > 2) Is the fact that the debugger jumps into
    > CDPServer::AddPlayerToGroup() related to me using a bad approach, or
    > can I chalk it up to my rather old debugger? When running, after all,
    > things happen the way they are suppose to.


    I'd be surprised if a debugger got this wrong, I'd suspect some subtle bug
    that only manifests itself in a debug build.

    Have you compared the value of the this pointer when when you convert it to
    void with the value when you cast back from void to CDPServer*, they should
    be the same.

    john
     
    John Harrison, Mar 1, 2004
    #2
    1. Advertising

  3. Bj?rn Toft Madsen

    Heinz Ozwirk Guest

    "Bj?rn Toft Madsen" <> schrieb im Newsbeitrag news:...
    : Hi all,
    :
    : The network library I use communicates with my app using a callback
    : function. This callback function is called with the type of message, a
    : void pointer to he actual message and a user defined void pointer.
    :
    : The callback function expected is a standard C callback function. I
    : have used a static class function, which I certainly know is not
    : entirely correct. Bear with me, I don't think my troubles are related
    : to this hack.
    :
    : When the class is constructed, I set up the network library by
    : supplying the address of the static method as the callback and I also
    : pass along "this" as the user defined void pointer.
    :
    : In the static method, the following takes place:
    : HRESULT CDPServer::MessageHandler(PVOID pvUserContext, DWORD
    : dwMessageType, PVOID pMessage) {
    : switch(dwMessageType) {
    : case DPN_MSGID_ADD_PLAYER_TO_GROUP:
    : return ((CDPServer*)pvUserContext)->AddPlayerToGroup((PDPNMSG_ADD_PLAYER_TO_GROUP)pMessage);
    : break;
    : //many more cases
    : }
    : }
    :
    : As you can see, I cast the user defined pointer to CDPServer*.
    :
    : The issue is that naturally all these "event handlers" like
    : AddPlayerToGroup() are meant to be overridden in sub-classes of
    : CDPServer:
    : class CMyServer : public CDPServer {
    : protected:
    : //other stuff
    : virtual HRESULT AddPlayerToGroup(/*...*/);
    : }
    :
    : So in effect, my static message handler casts the pointer to
    : CMyServer's super class (CDPServer) and then calls the overridden
    : function AddPlayerToGroup()

    A problem I see is your sequence of casts:

    CCMyServer* --> void* --> CDPServer*

    This might not always work. Try static_cast<CDPServer*>(this) instead of simply this.

    HTH
    Heinz
     
    Heinz Ozwirk, Mar 2, 2004
    #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.

Share This Page