Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Callback functions in classes

This topic is 6015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello again. In my display class to encapsulate DirectDraw7 I am trying to create a function that will enumerate through all the available display modes that the vid card is capable of. To do this you call lpdd->EnumDisplayMode(0, NULL, NULL, CallbackFunction); . The problem I''m running into is calling this function and passing one of my member functions as the callback. If the callback is created outside of the class it is defined as this: HRESULT WINAPI EnumModesCallbackBlah(LPDDSURFACEDESC2 lpds, LPVOID lpContext); I can call the enumerate function and pass this as the callback and the compiler is happy. What I''m trying to do is this: HRESULT WINAPI CDisplay::EnumModesCallbackBlah(LPDDSURFACEDESC2 lpds, LPVOID lpContext); When I make the call to enumerate the display modes with my class member I get the following error: f:\VS .NET Projects\DX1\Display.cpp(57): error C2664: ''IDirectDraw7::EnumDisplayModes'' : cannot convert parameter 4 from ''HRESULT (LPDDSURFACEDESC2,LPVOID)'' to ''LPDDENUMMODESCALLBACK2'' What makes the first enum function of type LPDDENUMMODESCALLBACK2 and not my member function? Can I do this? If so how? Thanks in advance!

Share this post

Link to post
Share on other sites
Unfortunatly, you can''t use a class method as a callback function.

The cause:

You probably know that a class is a data structure with some function binded to them. a C equivalent of a C++ method

int MyClass::DoThat(int num); // C++ class method

would be

int DoThat(MyClass this, int num); // C equivalent with a struct

That''s right, C++ when calling a member, is calling a regular function with a hidden parameter being the target object.

The solution:
You can use a regular function (these include statics) as a callback function and then map calls to an object and its method. But you will have to implement some mechanism to map the callback to the right object.

If you have done MFC before, you probably understand what I mean, but in your case it will be much more simpler because you will only have one object and one event to track.

Hope it''ll help

Share this post

Link to post
Share on other sites
Thanks Indirect, that was exactly what I needed to get it working. Well not the ONLY thing but it gave me the needed mentality to finish the job. Made the callbacks static, at the places where the calls needed to be made i simply passed the this pointer as part of the callbacks arguements which in turn casted back inside the callback so I could grab access to the rest of the class... not pretty looking but it got the job done :D

[edited by - xtrmntr on June 24, 2002 4:47:20 AM]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!