Jump to content
  • Advertisement
Sign in to follow this  
Deathscythe

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.

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
Advertisement
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
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 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
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.

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!