By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

Callback to member function

P: n/a
Hi
Iam trying to make a class where you can register callbacks to member
functions of other classes.
I declared the functions I wish to use like this:

void SetMouseProc(ONMOUSEPROC proc, void* pUserData);

where ONMOUSEPROC is this type:

typedef void (*ONMOUSEPROC) (void* pUserData, int iX, int iY);

The type is supposed to "emulate" the __thiscall calling convention, where
the first parameter is always the "this" pointer to the class instance.
That way i should be able to pass any member function to this function, and
the this pointer as userdata and it should work.
Unfortunately it does not.

This is how I call SetMouseProc:

void (CGame::*pf3)(int,int)=&CGame::OnMouse;
ONMOUSEPROC b=*(ONMOUSEPROC*)&pf3;
m_Input.SetMouseProc(b,g_pGame);

For some reason, directly casting the &CGame::OnMouse to (ONMOUSEPROC) didnt
work, so I had to "force" it to the other type :)

The code above is syntactically correct ( and admittedly evil looking ;/ ),
but it crashes as soon as one tries to actually call the callback function.
Any ideas how I could either fix my problem or replace my current hack
solution by a cleaner method?

Thanks in advance
Aug 2 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Frank Neuhaus wrote:
Iam trying to make a class where you can register callbacks to member
functions of other classes.
I declared the functions I wish to use like this:

void SetMouseProc(ONMOUSEPROC proc, void* pUserData);

where ONMOUSEPROC is this type:

typedef void (*ONMOUSEPROC) (void* pUserData, int iX, int iY);
[...]


Your question is covered in the FAQ.
Aug 2 '05 #2

P: n/a
Hmm ok thanks ;)
Just wondering:
Ive just been playing around with asm a little and I managed to fake the
thiscall calling convention properly by changing my stuff to this:
typedef void (_cdecl *ONMOUSEPROC) (int iX, int iY);

now a call to this function works like this:

mov esi,esp ;

mov eax,dword ptr [dwY] ;// param 2

push eax ;

mov ecx,dword ptr [dwX] ;// param 1

push ecx ;

mov ecx, dword ptr[userdata];// userdata contains the this pointer to the
class that was passed to the SetMouseProc function earlier

mov edx,dword ptr [this] ;

call dword ptr [edx+8] ; // just the location of the function inside the
class

Now this code works. The callback is issued correctly. Obviously a very
unclean way of doing it. But what Iam asking myself is: Why is there no way
in C++ to do the same? Why shouldnt the user be allowed to "fake" thiscalls?

"Victor Bazarov" <v.********@comAcast.net> schrieb im Newsbeitrag
news:_o*******************@newsread1.mlpsca01.us.t o.verio.net...
Frank Neuhaus wrote:
Iam trying to make a class where you can register callbacks to member
functions of other classes.
I declared the functions I wish to use like this:

void SetMouseProc(ONMOUSEPROC proc, void* pUserData);

where ONMOUSEPROC is this type:

typedef void (*ONMOUSEPROC) (void* pUserData, int iX, int iY);
[...]


Your question is covered in the FAQ.

Aug 2 '05 #3

P: n/a
Frank Neuhaus wrote:

Now this code works. The callback is issued correctly.


Try it with a virtual function.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 2 '05 #4

P: n/a
It is already virtual, because the CGame class is an abstract base class.

"Pete Becker" <pe********@acm.org> schrieb im Newsbeitrag
news:WP********************@rcn.net...
Frank Neuhaus wrote:

Now this code works. The callback is issued correctly.


Try it with a virtual function.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Aug 2 '05 #5

P: n/a
Frank Neuhaus wrote:
It is already virtual, because the CGame class is an abstract base class.


I didn't read your code closely enough. You've actually simulated a
simple virtual call there. The comment on the actual call is slightly
incorrect: it's not the location of the function inside the class, but
the location of the function pointer in the class's vtable. But that
particular calling sequence won't work with a non-virtual function, and
might or might not work when the this pointer points to a virtual base,
and won't work with an implementation that doesn't lay objects out the
same way as your compiler. Pointers to member functions hide all this
detail, all of which is implementation-specific, anyway.

This sort of asm-level hacking should only be done in the privacy of
your own home. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 3 '05 #6

P: n/a
Of course thats not a nice code and I wouldnt use it in my program anyway.
But iam simply wondering why its not possible to use the thiscall calling
convention just like any other?

"Pete Becker" <pe********@acm.org> schrieb im Newsbeitrag
news:8f********************@rcn.net...
Frank Neuhaus wrote:
It is already virtual, because the CGame class is an abstract base class.


I didn't read your code closely enough. You've actually simulated a
simple virtual call there. The comment on the actual call is slightly
incorrect: it's not the location of the function inside the class, but
the location of the function pointer in the class's vtable. But that
particular calling sequence won't work with a non-virtual function, and
might or might not work when the this pointer points to a virtual base,
and won't work with an implementation that doesn't lay objects out the
same way as your compiler. Pointers to member functions hide all this
detail, all of which is implementation-specific, anyway.

This sort of asm-level hacking should only be done in the privacy of
your own home. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

Aug 3 '05 #7

P: n/a
Frank Neuhaus wrote:
Of course thats not a nice code and I wouldnt use it in my program anyway.
But iam simply wondering why its not possible to use the thiscall calling
convention just like any other?


You'll have to ask Microsoft. There's no thiscall calling convention in
Standard C++. Just extern "C" and extern "C++", both of which
potentially involve the calling convention, among other things.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Aug 3 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.