Sign in to follow this  

String To Function Pointer

This topic is 4404 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

Do you think that this would ever be possible? Example: Say I have a function Foo, and it takes one string, which would be the name of a function with (void) as a parameter, and no return type. I create a void function Bar(), so then I pass "Bar" to Foo, to effectively create: Foo( "Bar" ) Then, perhaps, Foo would some how turn "Bar" into a function pointer, and then call the function. Perhaps something like this?
void Bar()
{
      // Do Stuff;
};

void Foo( std::string func )
{
      typedef void (*func)(void) thisFuncPtr; // Our function pointer.
};


So...is something like that already done and I'm just overlooking it, or what?

Share this post


Link to post
Share on other sites
C++ doesn't support this natively; some languages do. In C++ you'd need to maintain the list of function names yourself. Alternately if you have a restriction on where the functions you want to index occur, like all in a DLL, you can read the export table of the DLL at runtime to build your string to function map.

Share this post


Link to post
Share on other sites
It is possible to implement. You can use functors and functors factory that will bind function pointers to their names. I believe there is an article somewhere on GameDev or on CodeProject about implementing that kind of thing.

Share this post


Link to post
Share on other sites

This topic is 4404 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.

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