Sign in to follow this  

Call back function help.

This topic is 2079 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:
[CODE]
#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;
};
[/CODE].

Console Listener header:
[CODE]
#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);
};
[/CODE]

Console Listener CPP:
[CODE]
#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");
}
[/CODE]

also this error:
[CODE]
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 ==========
[/CODE]

would appreciate it if you could help me out.

Share this post


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

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 [b]eventFunction [/b]to return an [b]integer[/b] and take an [b]Event [/b]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 [i]static-cast the return type of the function, an integer[/i] to an Event. No wonder the compiler's screaming ;)

PS: Instead of passing around function pointers, you might want to consider Functors: [url="http://www.newty.de/fpt/functor.html"]http://www.newty.de/fpt/functor.html[/url] 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

This topic is 2079 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this