Jump to content

Function pointers in map


AggrorJorn
 Share

Recommended Posts

I am trying to call functions by storing them in a map with function pointers. I can't seem to figure out how the invoking of these functions are done:

 

init

pointerToFunction = &MyFunction;
myMap.insert(pair<string, void*>("Test", pointerToFunction));

 

somewhere in update()

myMap["Test"]();

 

I get the error here that the object preceding the parentheses should be a void pointer.

Link to comment
Share on other sites

I suspect the underlined bit is wrong, because it appears to be a standard void pointer, and not a function pointer.

 

myMap.insert(pair<string, void*>("Test", pointerToFunction));

 

For some reason, I'm thinking it should be this instead

 

myMap.insert(pair<string, void (* funcPtr)(void)>("Test", pointerToFunction));

 

The bit in red is whatever the parameter list for pointed-to function is, in this case, blank. And pointerToFunction should be declared in the same way. The bit in blue is the pointed-to function's return type, again, it must be declared identically

 

if your function took two int parameters, then try

 

myMap.insert(pair<string, void (* funcPtr)(int, int)>("Test", pointerToFunction));

 

and so on... I can't think if you need the (* funcPtr) bit or not because that's how you declare a function pointer, but I don't think you want a declaration here, so even I'm a bit confused. Remember function pointers can be confusing, even to the best of us, and even I'm a mere novice. Is there not an easier way to do what you're trying to do?

  • Upvote 1

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

@Mumbles:

Thanks for your time as well Mumbles. Let me first explain what I am trying to create: an ingame console. The first step is a simple command like 'DebugPhysics'. When this text is entered it should invoke a function without any parameters. The function pointers seem like a good approach. Once that works I also want to implement functions that take parameters. I will try this once I get home.

 

@Rick: I am not at home right now so it might be that I have typed wrong in this post.

//Header
std::map<string, void*> myMap;
void (*pointerToFunction)();

//CPP
void MyFunction()
{
printf("test");
}
//init in cpp
pointerToFunction = &MyFunction;
myMap.insert(pair<string,void*>(["Test", pointerToFunction));

Link to comment
Share on other sites

Haven't tested but like Mumbles is saying:

 

void (*pointerToFunction)();

std::map<string, pointerToFunction> myMap;

 

You want your value in the map to be the function pointer not just a void*. This should work then.

 

Adding parameters will be more difficult though as each different function pointer needs to be created for each signature. You'd be better off making this stuff in Lua I think as it's sooo much eaiser because functions can be passed around and stored like any other type, and parameters can all be dynamic too.

Link to comment
Share on other sites

function pointers are the only thing that I have tried so far. For commands that don't have any parameters it does the trick just fine I think. When I look at the best answer of this topic, there are plenty of solutions.

 

I could use Lua but I am worried that it gets really messy when I try to call stuff inside C++. I also want to improve my C++ skills rather then Lua. (everything I learn about lua is a nice extra). I will try the provided solution and will also try other solutions.

Link to comment
Share on other sites

Note that a couple of those solutions they have is the same as my event code I posted in the assets section that allows you to call member function pointers. The main problem these all suffer from is that the function sig has to be the same in each container OR you have to have multiple containers that are grouped by the function sig and manage that. This is why I suggested Lua where you don't have to mess around with that.

 

You could give all functions a parent class pointer (like Object*), and then have specific derived types that you down cast when calling and convert once inside to the type it's expecting. That way all function sigs are the exact same (1 Object* param), but calling them can passing derived objects from Object.

 

So like:

 

void ConsoleFunction1(Object* obj)

{

SpecificObject* object = (SpecificObject*)obj; // probably like dynamic_cast or whatever is safer

}

 

void ConsoleFunction2(Object* obj)

{

}

 

 

Since they all have the same sig then you can store them all in your map without problem. When calling it would be:

 

map["console1"]((Object*)new SpecificObject());

Link to comment
Share on other sites

The hashmap approach certainly sounds like it could work, but I've never tried using function pointers in this way before.

 

But I will warn you, all your function pointers in the same hashmap need to have the exact same parameter list and return type. A function pointer to a function that returns an int, is not the same type as a function pointer to a function that returns a float for example.

 

And two function pointers to functions that each return an int, but have different parameter lists are also not the same as each other.

 

However you can get around that by having a "void *" return type and a single "void *" argument. This way it's up to the individual function to cast that void pointer into a meaningful struct or object to work with, and then whatever it passes out, the calling code must then know what to convert that void pointer into.

 

It will probably cause quite a few errors at first as you accidentally cast these pointers into the wrong types, but in time, if the hashmap console approach works, you'll quickly adapt to using void pointers as a means of passing the data to and from your dynamic functions.

LE Version: 2.50 (Eventually)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...