Click to See Complete Forum and Search --> : how to call fuction having its name in a string
arunonnet
November 5th, 2001, 04:39 AM
How can I call a function, given its name as a string ?(i.e .suppose u have five diff functions..eg% add,sub,mul,div,rem and in ur main u get the name of the function to b called from the user as a string ...then how d u invloke the right function using the string which u have now in the main) Please dont say using an if loop..this is very trivial..think to our standards...
AlanGRutter
November 5th, 2001, 05:17 AM
Sounds to me like you need to use an associative array to store the string and a function pointer. The obvious way is to use an STL map, with the key as the function name and the value being a pointer to the function.
Regards
Alan.
arunonnet
November 8th, 2001, 11:12 AM
hello
thanx for hint. IM not aware of wat ur talking .
can u be more elobrate on assosiative array and STl mappings. Can i find any resource in net for these things.
regards
arun.
James Curran
November 8th, 2001, 01:29 PM
void Func1(int);
void Func2(int);
typedef void (*func_ptr)(int);
#include <map>
#include <string>
using std::map;
using std::string;
map<string, func_ptr> func_map;
// :
// :
func_map["Func1"] = Func1; // associate string "Func1" with a pointer to the function Func1()
Func_map["Func2"] = Func2;
// :
// :
string aFuncName = "Func1";
// :
func_ptr pFunc = func_map[aFuncName];
pFunc(3);
Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.
Graham
November 9th, 2001, 04:17 AM
Of course, this only works if all the functions to be mapped have the same signature. If not, things start to get a bit hairy....
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
James Curran
November 9th, 2001, 04:45 AM
Yes, but if they didn't, how would you call them?
Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.
Graham
November 9th, 2001, 05:29 AM
That's pretty much my point. The map solution has a BIG proviso: that the functions all have the same signature. If you can't meet that requirement, then the alternative solution is radically different to the original. Hence, the wrong choice here can really screw you up if/when the situation changes in the future.
I usually find that, as a solution starts piling on the provisos, then future problems become more likely and more difficult to solve.
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
Paul McKenzie
November 9th, 2001, 05:34 AM
Do all of the functions have the same signature (return type, number of parameters, and parameter types the same)?
If not, this is not a trivial task.
If the functions you want to call do have the same signatures, then use a map of a string to a function pointer, i.e
typedef int (*fptr)();
std::map<std::string, fptr>.
Regards,
Paul McKenzie
Igor Soukhov
November 9th, 2001, 05:45 AM
Old good question =) It resembles me last summer I spent behind of my computer at VC++ forum =)
Please - rate answer if it helped you
It gives me inspiration when I see myself in the top list =)
Best regards,
-----------
Igor Soukhov (Brainbench/Tekmetrics ID:50759)
igor@soukhov.com | ICQ:57404554 | http://soukhov.com
Russian Software Developer Network http://rsdn.ru
James Curran
November 9th, 2001, 06:22 AM
NO, *ANY* solution has that proviso. Even if we take the map out:func_ptr pFunc = GetFunc("MyFunc");
(*pFunc)(1,2,34);
We still have the problem of matching the parameters to the function. If we have various different methods of calling different functions, then at any particular spot in the program, we know which function we are going to call, so the translation is unnecessary. If we have several different classes of functions, say, some that take one parameter, and some that take two, then we just have two separate but identical problems, which can be handled by two maps.
Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.
Graham
November 9th, 2001, 06:31 AM
You may be right. The problem doesn't concern me, so I've not investigated all the ramifications.
My idea was that some variant of the Visitor pattern might possibly be usable, where the visitor is responsible for all the differences between signatures, and the accepter just provides classes that select the correct visitor. A pointer to the accepter base class could go in the map, then calling the function consists of visiting the appropriate entry.
I admit that that was a gut feeling for a solution and I haven't really investigated whether it would work in practise (for example, the vistor is responsible for obtaining the arguments AFTER the function has been identified). The point still remains, though, that IF there is a solution, THEN it's radically different to the proposed map.
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
James Curran
November 9th, 2001, 06:39 AM
Well, there is the solution used by the Message Maps in MFC, where we are not mapping string to functions, but WM_ codes to function, but there I believe deep in the bowels of MFC there's a giant switch statement to get all the parameters right. The only real point of the map is to put the information in one spot.
Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.