Jump to content
  • Advertisement
Sign in to follow this  
Enerjak

Call back function help.

This topic is 2291 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

OK, i had an urge to make event classes and/or call back functions in order to allow me to learn how to pretty much send custom events to my code list Ogre's listeners, or Windows Messages, or even Qt's signal/slot mechanism. Now, These classes seem to work, though I'm not sure that's like a signal sending though

Listener Header:

#pragma once
#include <string>
// event struct
struct Event
{
unsigned int id; // id of event
std::string eventName; // event name;
};
// call back functions
typedef int (__stdcall *EventFunction)(Event e);
class Listener
{
public:
Listener(){}

~Listener(){}
virtual void addEvent(const Event& e) = 0;
virtual bool addCallBackFunction(EventFunction fun) = 0;
};
.

Console Listener header:

#pragma once
#include "Listener.h"
#include <d3d9.h>
class ConListener : public Listener
{
public:
ConListener();
~ConListener();
void addEvent(const Event& e);
void changeTitle();
bool addCallBackFunction(EventFunction fun);
};


Console Listener CPP:

#include "ConListener.h"
ConListener::ConListener()
{
}
ConListener::~ConListener()
{
}
bool ConListener::addCallBackFunction(EventFunction fun)
{
Event e = static_cast<Event>((*fun));

return true;
}
void ConListener::addEvent(const Event& e)
{
if (e.eventName == "Change Title")
{
this->changeTitle();
}
}
void ConListener::changeTitle()
{
SetConsoleTitleA("Dog");
}


also this error:

1>ConListener.cpp
1>c:\users\tim sweeny\documents\visual studio 2008\projects\x\candydispenser\conlistener.cpp(15) : error C2440: 'static_cast' : cannot convert from 'int (__stdcall *)(Event)' to 'Event'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>Build log was saved at "file://c:\Users\tim Sweeny\Documents\Visual Studio 2008\Projects\x\CandyDispenser\Debug\BuildLog.htm"
1>CandyDispenser - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


would appreciate it if you could help me out.

Share this post


Link to post
Share on other sites
Advertisement
[color=#000088]bool[color=#000000] [color=#660066]ConListener[color=#666600]::[color=#000000]addCallBackFunction[color=#666600]([color=#660066]EventFunction[color=#000000] fun[color=#666600])
[color=#666600]{
[color=#660066]Event[color=#000000] e [color=#666600]=[color=#000000] [color=#000088]static_cast[color=#666600]<[color=#660066]Event[color=#666600]>((*[color=#000000]fun[color=#666600]));

I'm not really sure what you're trying to do.

Since you did the typedef on the eventFunction, I'm guessing you want the eventFunction to return an integer and take an Event as a parameter since the function signature is:
typedef int (_stdcall *eventFunction) (Event e);


I don't see you passing an Event to the function as the function definition states. You're trying to static-cast the return type of the function, an integer to an Event. No wonder the compiler's screaming ;)

PS: Instead of passing around function pointers, you might want to consider Functors: http://www.newty.de/fpt/functor.html which are a much neater and less hacky way to solve this problem :)


Anyway,
Hope this helps

Share this post


Link to post
Share on other sites
Thank you very much. man, i've got ALOT of work to do on this engine Tim Sweeney took 3 years to make the first unreal engine, me it'll take 25

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!