Sign in to follow this  

Help with Functors

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

(Sorry if this is the wrong forum, but I was unable to post a reply in the Article Feedback forum) My question is regarding the Functors as implemented in superpig's enginuiy. I have the following functors defined:
[SOURCE]
class IFunctor : public IMMObject
{
public:
   virtual void operator ()()=0;
};

template<class T>
class IMMObjFunctor : public IFunctor
{
protected:
   CMMPointer<T> obj;
   typedef int (T::*funcType)();
   funcType func;
public:
   AUTO_SIZE;

   IMMObjFunctor(T *o, funcType f) 
   { obj=o; func=f; }

  void operator ()() 
   { (obj->*func)(); }
};
[/SOURCE]
and I am attempting to create a functor to register as a callback function for use in my eventmanager.
[SOURCE]
IMMObjFunctor<CGxD3DSprite> KeyboardHandler(this, &CGxD3DSprite::HandleMovement)
	CEventManager::GetSingleton().RegisterCallback(EVENT_KEY_DOWN, KeyboardHandler);
	CEventManager::GetSingleton().RegisterCallback(EVENT_KEY_UP, KeyboardHandler);
[/SOURCE]
but I end up getting the following error: c:\Programming\StarFire\src\gfx\GxD3DSprite.cpp(32) : error C2664: 'IMMObjFunctor<T>::IMMObjFunctor(T *,IMMObjFunctor<T>::funcType)' : cannot convert parameter 2 from 'void (__thiscall CGxD3DSprite::* )(void)' to 'IMMObjFunctor<T>::funcType' with [ T=CGxD3DSprite ] and [ T=CGxD3DSprite ] Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast I've already tried all explicit casting but they all give the same message. Can anyone shed some light on what I can do?

Share this post


Link to post
Share on other sites
IMMObjFunctor<CGxD3DSprite> KeyboardHandler(this, &CGxD3DSprite::HandleMovement)

"cannot convert parameter 2 from 'void (__thiscall CGxD3DSprite::* )(void)' to 'IMMObjFunctor<T>::funcType'"

You are trying to pass a void (__thiscall CGxD3DSprite::* )(void) when it wants a int (__thiscall CGxD3DSprite::* )(void) (See: typedef int (T::*funcType)())

I'm in a hurry so that's all I have time to say. Hope it helps.

- Benny -

Share this post


Link to post
Share on other sites
Also, you might want to check out boost::function and boost::bind. They offer a much more flexible way of dealing with function objects. Anything that takes a boost::function as a parameter will work with normal functions, boost function objects or even any object that defines a call operator. It works via templates so you don't have to define individual functor classes for each type of functor you want.


boost::function<int ()> KeyboardHandler = boost::bind(&CGxD3DSprite::HandleMovement, this);

CEventManager::GetSingleton().RegisterCallback(EVENT_KEY_DOWN, KeyboardHandler);

// Example of a functor with parameters
boost::function<int (KeyEvent)> KeyboardHandler = boost::bind(&CGxD3DSprite::HandleMovement, this, _1);

CEventManager::GetSingleton().RegisterCallback(EVENT_KEY_DOWN, KeyboardHandler);

Share this post


Link to post
Share on other sites

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