Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Casting a void pointer to base class and calling overridden function ... allowed? (http://www.velocityreviews.com/forums/t281765-casting-a-void-pointer-to-base-class-and-calling-overridden-function-allowed.html)

Bj?rn Toft Madsen 03-01-2004 01:46 PM

Casting a void pointer to base class and calling overridden function ... allowed?
 
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)pMe ssage);
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

John Harrison 03-01-2004 06:28 PM

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

"Bj?rn Toft Madsen" <sunbeam60@hotmail.com> wrote in message
news:9349844c.0403010546.65640866@posting.google.c om...
> 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



Heinz Ozwirk 03-02-2004 08:18 AM

Re: Casting a void pointer to base class and calling overridden function ... allowed?
 
"Bj?rn Toft Madsen" <sunbeam60@hotmail.com> schrieb im Newsbeitrag news:9349844c.0403010546.65640866@posting.google.c om...
: 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)pMe ssage);
: 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


All times are GMT. The time now is 10:59 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.