# [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
{
public:
void RegisterFunctions();
private:
mfpmap mHoverFunctionMap;

void System::AddFunction(MouseEvent a_Event, std::string a_MenuItemName, menufp a_Function)
{
}

void System::RegisterFunctions()
{
}

{
}

{
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 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 on other sites
Thank you it works now
and i'll look into the other methods now because that is some ugly code but at least it gets the job done

##### Share on other sites
Did you know that you can also have function references?! [wow]

##### Share on other sites
Quote:
 Original post by iMalcDid 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 on other sites
Quote:
Original post by implicit
Quote:
 Original post by iMalcDid 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 on other sites
Rather than go too much off topic here, I've started my own thread about function references.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628395
• Total Posts
2982432

• 10
• 9
• 19
• 24
• 9