Archived

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

LTJ

C++ Problems with Callbacks

Recommended Posts

LTJ    122
For my game, I needed some kind of command handler to be used in the console, and it should be some kind of object oriented (most handlers must have access to a class), without being a hack. I also wanted the system to be dynamic and have a table of all commands plus simple usage message. It seemed like I had found the perfect solution: I had an empty base class called CmdCaps, which was to inherit the ability to be a command handler class. I had an std:map with the command as the key, and a struct which containted a pointer to a CmdCaps class, a pointer to a a member function of a CmdCaps class, which was the event handler function, and a simple usage string, as the keydata. That way it should have been possible to register more than one function for one object. However I can''t pass pointers to member functions of CmdCaps derived classed to my system. It seems there''s no way to typecast them. Any ideas how to fix that or and other suggestions for a good command system?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The problem using callbacks with pointer to memeber functions is that member functions are dependant on the object they are comming from. The problem is that APIs aren''t design with object oriented programming in mind. Mabye this will change in the future... read on here for more information about se subject (and some workarounds)

http://www.parashift.com/c++-faq-lite/pointers-to-members.html

Share this post


Link to post
Share on other sites
Sly    128
Borland solved this problem with their own solution because C++ does not nicely support pointers to member functions. You can do it, but I can never remember the code because it is quite convoluted. Borland implemented the "__closure" keyword in C++ Builder and the "of object" keyword in Object Pascal to natively support pointers to member functions.

Steve ''Sly'' Williams  Monkey Wrangler  Krome Studios
turbo game development with Borland compilers

Share this post


Link to post
Share on other sites
Fruny    1658
Manipulating bound pointers to member functions (affectionately known as BPMF, the thing that result of writing object.*PMF or ptr_object->*PMF) is an extension. According to the standard, BPMF have NO type, and the only thing you can do with them is call them.

g++ does support casting BPMF to regular pointers, but this is also an extension.

Share this post


Link to post
Share on other sites