Sign in to follow this  
projectghost

[C++] Map function pointers

Recommended Posts

I know I know function pointers are evil But in this case for what I want to do it comes in handy were it not that this is creating an error :( please help me solve it good people of Gamedev
class System
{
	typedef bool (*menufp)();
	typedef std::map<std::string, menufp> mfpmap;
public:
	void AddFunction(MouseEvent a_Event, std::string a_MenuItemName, menufp a_Function);
	bool ExecuteFunction(MouseEvent a_Event, std::string a_MenuItemName);
	void RegisterFunctions();
	bool AddButton();
private:
	mfpmap mHoverFunctionMap;
void System::AddFunction(MouseEvent a_Event, std::string a_MenuItemName, menufp a_Function)
{
	mHoverFunctionMap[a_MenuItemName] = a_Function;
}

void System::RegisterFunctions()
{
	AddFunction(MouseEvent::HOVER, "root.Window.Window_Button_2", &System::AddButton);
}

bool System::ExecuteFunction(MouseEvent a_Event, std::string a_MenuItemName)
{
	return mHoverFunctionMap[a_MenuItemName]();
}

bool System::AddButton()
{
	return true;
}
which gives me the following error in RegisterFunctions(): system.cpp(111) : error C2664: 'System::AddFunction' : cannot convert parameter 3 from 'bool (__thiscall System::* )(void)' to 'System::menufp' 1> There is no context in which this conversion is possible

Share this post


Link to post
Share on other sites
Your function pointer typedef is meant for non-member functions (or static functions defined in the class), but System::AddButton is a member function. The syntax for non-static member functions are different, and they will also require an object when calling. It would look a little more like this:
    typedef bool (System::*menufp)();
and calling the function would look like
    (this->*mHoverFunctionMap[a_MenuItemName])();
assuming of course that this is referring to a System instance. Not the prettiest thing on the block eh? It also restricts you to (non-static) functions that are defined in your System class. You'll probably want to look at functors to make this a lot nicer, or avoid using (non-static) member functions.

I personally enjoy using boost:bind and boost::function. Check them out if you're not against using boost.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Did you know that you can also have function references?! [wow]
At first glance such a thing seems entirely useless. Any idea what the rationale was for introducing those? Oh, and is there such a thing as a data member reference?

Share this post


Link to post
Share on other sites
Quote:
Original post by implicit
Quote:
Original post by iMalc
Did you know that you can also have function references?! [wow]
At first glance such a thing seems entirely useless. Any idea what the rationale was for introducing those? Oh, and is there such a thing as a data member reference?
They have the same advantages over function pointers that regular references have over pointers. A function reference can't be reseated, i.e. it can be made to "point" for a different function, and it can't be NULL i.e. it must point to a function. Just like a regular reference, you call it without the dereferencing operator. So nope, they're not useless.
In fact from what I can tell we really should be prefering them over function pointers for the same reasons we prefer references over pointers! But for some reason hardly anyone seems to know about them. I actually use them appropriately in my software 3D engine, since as with regular references they're actually easier to work with.

Yes you can also have members that are references or even const-references. However such a reference must always be initialised in the constructor of that struct/class.

Share this post


Link to post
Share on other sites

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