   Function Pointers
  Hi,I want to call a function pointer, and define to which funtion it goes by a char instead of just writing the function name down.Hard to explain, but the code below speaks for itselfFunction to be calledvoid AILevelOne(Monster *monster){monster->SetX(500);}I have this function which takes a function pointervoid Monster::Update(void(*AIUpdate)(Monster*)){AIUpdate(this);}Later on,  i have this which calls the function pointerMonster::Update(AILevelOne)// That worksBut i want thisMonster::Update(monster_properties.ai_func)   // ai_func is a char with    // "AILevelOne" And that doesnt workIs there a way to do this?[Edit: Made the post pretty - Oluseyi][Edited by - Oluseyi on January 20, 2005 7:02:21 AM]
  That might work if you used reflection because you can use a string to get a class from a class loader by its' name.  But the name of the function is very different from a function pointer.It would be easier if you typedef'ed your function prototype and stored a pointer to a function in your monster_properties class instead of the string.Take care to use a static function for your callback too, unless you can be really sure the object whose function you are calling is going to be alive.If you want different monsters to have different AI associated, it might be worth while creating an AI class that supports a particular interface and associate the monsters with that - or use inheritence and specialise AI for certain monsters.   ;  Why can't you make monster_properties.ai_func a function pointer? That would be the easiest/cleanest solution.If you really do want to call a function by a string, my best guess would be to use a map, like so:typedef void(*function_ptr)(Monster*);std::map<std::string, function_ptr> functions;You would then need to fill the map up with all the possible fuctions name + adresses, so things could get messy! But you could then call them like this:functions["AILevelOne"];HTH   ;Quote:Original post by blizz107Is there a way to do this?yes lots of ways of doing it, first i would download and change to using boost::function, then your not just restricted to using plain pointer to functions, you can use pointer member functions, functors anything that conforms to the signature you've set for instances of boost::function.i would change this:void Monster::Update(void(*AIUpdate)(Monster*)) { /* ... */ }to a template member function:template < typename UnaryFunction >void Monster::Update(UnaryFunction updator) { updator(*this); }then you can use anything invokable not just plain pointer functions which is restricting.In-order to invoke a function via a string using the same "Update" method, as already mentioned a map of string to callable entity is one way (pointer to free/member function, functor etc), you just need to overload the member function Update to take a string, i'd suggest if your compiler supports it use hash_map instead of map (hash_map is not currently part of the c++ standard its an STL extension) of std::string to boost::function (so you can store any callable entity that supports the signature, not just pointer to free-function), with all this in mind:#include <boost\function.hpp>#include <string>#include <map>struct monster {typedef boost::function< void (monster&) > updator;typedef std::map< std::string, updator > func_map;typedef func_map::iterator iterator;typedef func_map::const_iterator const_iterator;typedef std::pair< iterator, bool > result_pair;typedef func_map::value_type value_type;private:func_map func;public:void setX(int i) {}result_pair set_updator(const std::string& name, const updator& up) {return func.insert(std::make_pair(name, up));}void update(const char* const s) {func(*<span class="cpp-keyword">this</span>);<br>}<br><br><span class="cpp-keyword">void</span> update(<span class="cpp-keyword">const</span> std::string&amp; s) {<br>func(*<span class="cpp-keyword">this</span>);<br>}<br><br><span class="cpp-keyword">template</span> &lt; typename UnaryFunc &gt;<br><span class="cpp-keyword">void</span> update(UnaryFunc updater) {<br>updater(*<span class="cpp-keyword">this</span>);<br>}<br><br>};<br><br><span class="cpp-keyword">void</span> AILevelOne(monster&amp; m) {<br>   m.setX(<span class="cpp-number">500</span>);<br>}<br><br><span class="cpp-keyword">int</span> main() {<br><br>   monster m;<br><br>   m.set_updator(<span class="cpp-literal">"AILevelOne"</span>, AILevelOne);<br><br>   m.update(<span class="cpp-literal">"AILevelOne"</span>); <span class="cpp-comment">//by string</span><br><br>   m.update(AILevelOne); <span class="cpp-comment">//by reference to function i.e. void (&amp;)(monster&amp;);</span><br><br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>You might also want to look into the <a href="http://c2.com/cgi/wiki?ObserverPattern">observer design pattern</a> (and its variants) as another alternative.<br><br>note: prefer references over pointers, &#111;nly use pointers when necessary.;  Thanks you all for the help :) I’m going to look into each solution, An AI class could be a good idea and I’m going to take a look into std::map. Also I’ll consider if its an option to make monster_properties.ai_func a function pointer. Third: snk_kid, I’m going to look the code you wrote all over. Thanks for putting so much thought into this :) I’m not too experienced yet, so I’m going to take my time and lookup some things you guys wrote which i haven't used before. I’ll let you know how it goes .