Jump to content
  • Advertisement

Archived

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

Nazghul

Messagehandler with C++?

This topic is 6934 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 guys! Recently i wrote a really cool (i think so) DInput DLL. It worked really good but now, as i''m writing a C++ UI class i get a problem with it: The DLL calls a message handler everytime the user made some input. Here is a quick example: void input_SetHandler(void (*handler)(long i)) { handler(2); } void myHandler(long i) { cout << i; } ... input_SetHandler(myHandler); This works pretty well. But know i need somethin like this: class myClass_c { public: void myHandler(long i) { cout << i; } } myClass; input_SetHandler(myClass.myHandler); That produces the following compiler error: C2664: Cannot convert parameter 1 from ''void (long i)'' to ''void (__cdecl *)(long i)'' I don''t know if that''s the exact phrase because i''m using the german version of the Microsoft Visual C++ compiler.

Share this post


Link to post
Share on other sites
Advertisement
Please repost that error in German. For some reason I wish to see it.

I think the problem is that you need this line,

input_SetHandler(myClass.myHandler); ,

to be something like this,

input_SetHandler(&(myClass.myHandler));

Though I''m not sure if the ++ in C will get in the way.

Actually, seems like a place for virtual functions. Below is a link to a recent article on using event processing by Steven Hugg for flipcode.com. It uses virtual functions. Events are essentially the same as messages (at least the way I look at it). I''ve just implemented this myself for C++ (I''ve used something very similar at work for 6 years now, but in C). Don''t know if its right for you, especially since you have already developed something, but take a look.

http://www.flipcode.com/tfiles/steven03.shtml.

Mike Roberts
aka milo
mlbobs@telocity.com

Share this post


Link to post
Share on other sites
The problem is that member functions look different than, global functions. This is because the functions need to know which object they belong to, this information is implicitly sent to the function in the ''this'' variable. If you declare your handler function as static you should be able to send it to input_SetHandler(). The problem with this is that when the Input dll calls the function it won''t know which object it belongs to so you cannot use any nonstatic member variables unless you supply that information some other way.

This should work:

class myClass_c
{
public:
static void myHandler(long i)
{
cout << i;
}
} myClass;

input_SetHandler(myClass.myHandler);





- WitchLord

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!