CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Nov 2001
    Posts
    6

    how to call fuction having its name in a string

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



  2. #2
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: how to call fuction having its name in a string

    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.


  3. #3
    Join Date
    Nov 2001
    Posts
    6

    Re: how to call fuction having its name in a string

    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.


  4. #4
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: how to call fuction having its name in a string


    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.

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: how to call fuction having its name in a string

    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
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: how to call fuction having its name in a string

    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.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: how to call fuction having its name in a string

    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
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how to call fuction having its name in a string

    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


  9. #9
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: how to call fuction having its name in a string

    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)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  10. #10
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: how to call fuction having its name in a string

    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.

  11. #11
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: how to call fuction having its name in a string

    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
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  12. #12
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: how to call fuction having its name in a string

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured