Jump to content

C++: input string is the function I want to run


AggrorJorn
 Share

Recommended Posts

After some googling I haven't been able to find what I was looking for.

What I basicly want to achieve is enter a string and that string is literally the function that I want to run. The thing is I don't want to use a swich that compares if the string is equal to a certain value, and then execute a function.

 

I know this is possible but I haven't found a way of doing this. Can someone please send in the right direction?

 

Thanks

Link to comment
Share on other sites

You can't, at least not directly... but there are options, from the top of my head:

- use std::map

- make your own look-up table and hashing function for strings

- use managed c++, it should be similar to how it's done in c#

- add reflection to c++ ala www.extreme.indiana.edu/reflcpp/

- use LUA to call your c++ functions

- put your functions inside a .dll, then use GetProcAddress() to get a function pointer by string name

- i bet the boost lib has something to this effect

- probably other ways

 

 

... I recommend to just use the STL map container for this. smile.png

Link to comment
Share on other sites

Yeah, in C++ you can't do this directly. I second paramecij to store what you want in a stl map and do it that way. If you want access to C++ members use the Event code I posted in the assets and then you'll be calling events, which you've tied to member functions for created objects, but yeah either way you have to do plumping work like setting up the stl map.

Link to comment
Share on other sites

I'm telling you, I'm just telling you :), use the event file I put up. You don't even fully have to understand the details, just use it in a couple tests and I just think you'll love it. I use it all over the place for soooo many things. It's so handy when programming in C++. It opens up an entire layer of communication between objects.

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