473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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
Jul 22 '05 #1
2 2514

"Bj?rn Toft Madsen" <su*******@hotmail.com> wrote in message
news:93**************************@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
Jul 22 '05 #2
"Bj?rn Toft Madsen" <su*******@hotmail.com> schrieb im Newsbeitrag news:93**************************@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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
7
by: James Brown | last post by:
I have two classes, a base class, and a class base { public: base(); virtual void foo() = 0; }; class derived : public base
3
by: joe bruin | last post by:
hello all. i am trying to get rid of some warnings and do "the right thing". although in this particular case, i am not sure what the right thing is. the code: typedef struct {
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
5
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.