# Help with Functors

This topic is 5148 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 on other sites
typedef int (T::*funcType)();

The member function you pass in should have a return type of int, but from that error message it looks like its return type is void.

EDIT: benstr beat me to it.

##### 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 parametersboost::function<int (KeyEvent)> KeyboardHandler = boost::bind(&CGxD3DSprite::HandleMovement, this, _1);CEventManager::GetSingleton().RegisterCallback(EVENT_KEY_DOWN, KeyboardHandler);

##### Share on other sites
benstr, joanusdmentia: Thanks guys! That was exactly it!
Kaijin: I am aware of the Boost library but would like to see if I can implement as much of my own engine as I can. Thanks anyways.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632935
• Total Posts
3009304
• ### Who's Online (See full list)

There are no registered users currently online

×