Thread: Convert string to function

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    Question Convert string to function

    Is there a way to convert a string to a function? I want to basically pass different function names as string as part of a communication protocol and then use one of the string element as a function name.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by justcurious View Post
    Is there a way to convert a string to a function? I want to basically pass different function names as string as part of a communication protocol and then use one of the string element as a function name.
    No.

    You'll need a table from function name to function pointer and look it up. It's unsafe if it was possible anyway; imagine someone deciding to call system("rm -rf /"); as function...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    It would be a lot safer for both sides to agree what the valid list of functions to be called is, then associate those functions via an enum.

    It's possible to do it in a couple of ways, but as EVOEx notes, it is not something you would want unchecked access to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    18
    Assuming that the program runs under the same privileges as usual for that user, the possible "Read Mail Really Fast"-command wouldn't be a new security problem. And depending on what you want to do, it might be actually an easy (although not portable) solution to use the system(3) (or whatever MS-Windows equivalent) call, and make separate executable files out of your functions.
    I had a similar problem once, when I wrote a Matlab-alike interface for an image processing framework, and I ended up with a std::map<std::string, function_object_type>, where the function_object_type was a base for all functors I allowed to be called that way. For passing a variable amount and different types of parameters I parsed the input using Yacc&Lex, pushing them on a std::stack<functor_variable_type>, which would then be used by the functor called. In had an alternative idea, but it included overloading "operator,", which I decided, in the interest of my advisor's sanity, not to do. Might have been funny, though. Watching him go mad, I mean.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by EVOEx View Post
    No.
    Of course you can. How do you think any command line interface works? It takes a string and looks up what it's supposed to do with it.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by quzah View Post
    Of course you can. How do you think any command line interface works? It takes a string and looks up what it's supposed to do with it.


    Quzah.
    It doesn't look up a function from that; it looks up a file from that and executes the file.

    But it might be possible, just not portably. If the linker leaves in symbol names, it's possible to look up the addresses of them. But anyone using that should be banned from coding.
    (Except for libraries of course, in that case it is even common in Windows)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct foo {
        functionpointer f;
        char * functionname;
    };
    What were we just saying again?


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM