string convert to function name

Discussion in 'C++' started by Gary Wessle, Nov 9, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    is there a way to convert a string to a function name and fire it. like

    void his_fun(){
    cout << "his is here" << endl;
    }

    vector<string> vec;
    vec.push_back("his");
    vec.push_back("me");

    for(i=0; i<vec.size(); i++)
    string var = vec + _fun;
    fire(var); and it will fire the routine "his_fun?

    is so, how?

    thanks
    Gary Wessle, Nov 9, 2006
    #1
    1. Advertising

  2. Gary Wessle

    Rolf Magnus Guest

    Gary Wessle wrote:

    > Hi
    >
    > is there a way to convert a string to a function name and fire it.


    No.
    Rolf Magnus, Nov 9, 2006
    #2
    1. Advertising

  3. Gary Wessle

    benben Guest

    Gary Wessle wrote:
    > Hi
    >
    > is there a way to convert a string to a function name and fire it. like
    >
    > void his_fun(){
    > cout << "his is here" << endl;
    > }
    >
    > vector<string> vec;
    > vec.push_back("his");
    > vec.push_back("me");
    >
    > for(i=0; i<vec.size(); i++)
    > string var = vec + _fun;
    > fire(var); and it will fire the routine "his_fun?
    >
    > is so, how?


    #include <string>
    #include <map>
    #include <iostream>

    typedef void (*func_ptr)(void);

    std::map<std::string, func_ptr> functions;

    void fun1()
    {
    std::cout << "fun1\n";
    }

    void fun2()
    {
    std::cout << "fun2\n";
    }

    int main()
    {
    functions["fun1"] = &fun1;
    functions["fun2"] = &fun2;

    std::string name;
    std::cin >> name;

    functions[name](); // invoke
    }



    Ben
    benben, Nov 9, 2006
    #3
  4. It's interesting.
    Osamede.Zhang, Nov 9, 2006
    #4
  5. Gary Wessle

    missdeer Guest

    Yeah, sereral C++ wrappers for some script languages are implemented in this
    way.

    "benben" <benhonghatgmaildotcom@nospam>
    ??????:4552dfdf$0$29167$...
    > Gary Wessle wrote:
    >> Hi
    >>
    >> is there a way to convert a string to a function name and fire it. like
    >>
    >> void his_fun(){
    >> cout << "his is here" << endl;
    >> }
    >>
    >> vector<string> vec;
    >> vec.push_back("his");
    >> vec.push_back("me");
    >>
    >> for(i=0; i<vec.size(); i++)
    >> string var = vec + _fun;
    >> fire(var); and it will fire the routine "his_fun?
    >>
    >> is so, how?

    >
    > #include <string>
    > #include <map>
    > #include <iostream>
    >
    > typedef void (*func_ptr)(void);
    >
    > std::map<std::string, func_ptr> functions;
    >
    > void fun1()
    > {
    > std::cout << "fun1\n";
    > }
    >
    > void fun2()
    > {
    > std::cout << "fun2\n";
    > }
    >
    > int main()
    > {
    > functions["fun1"] = &fun1;
    > functions["fun2"] = &fun2;
    >
    > std::string name;
    > std::cin >> name;
    >
    > functions[name](); // invoke
    > }
    >
    >
    >
    > Ben
    missdeer, Nov 9, 2006
    #5
  6. Gary Wessle

    mlimber Guest

    benben wrote:
    > Gary Wessle wrote:
    > > Hi
    > >
    > > is there a way to convert a string to a function name and fire it. like
    > >
    > > void his_fun(){
    > > cout << "his is here" << endl;
    > > }
    > >
    > > vector<string> vec;
    > > vec.push_back("his");
    > > vec.push_back("me");
    > >
    > > for(i=0; i<vec.size(); i++)
    > > string var = vec + _fun;
    > > fire(var); and it will fire the routine "his_fun?
    > >
    > > is so, how?

    >
    > #include <string>
    > #include <map>
    > #include <iostream>
    >
    > typedef void (*func_ptr)(void);
    >
    > std::map<std::string, func_ptr> functions;
    >
    > void fun1()
    > {
    > std::cout << "fun1\n";
    > }
    >
    > void fun2()
    > {
    > std::cout << "fun2\n";
    > }
    >
    > int main()
    > {
    > functions["fun1"] = &fun1;
    > functions["fun2"] = &fun2;
    >
    > std::string name;
    > std::cin >> name;
    >
    > functions[name](); // invoke
    > }


    This is exceedingly dangerous. The [] lookup operation is non-const
    because it does an insert cum default intialization if the key is not
    found, and since the result (the function pointer) is immediately
    dereferenced and invoked, this would mean dereferencing a null pointer,
    which, as you know, is bad bad bad. The safe alternative is to use
    map::find for lookups (unless you're absolutely sure that the string is
    one of your keys, but even then, I'd prefer to err on the side of
    caution to prevent future changes from creating problems).

    Cheers! --M
    mlimber, Nov 9, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    805
    Roedy Green
    Jan 2, 2006
  2. Moonbeam66
    Replies:
    2
    Views:
    422
    Moonbeam66
    Jun 7, 2005
  3. cutecutemouse
    Replies:
    2
    Views:
    345
    cutecutemouse
    Jul 13, 2008
  4. Dr. Phillip M. Feldman

    how to convert string function to string method?

    Dr. Phillip M. Feldman, Dec 7, 2009, in forum: Python
    Replies:
    8
    Views:
    334
    Bruno Desthuilliers
    Dec 8, 2009
  5. Replies:
    2
    Views:
    169
    John Bokma
    Aug 30, 2013
Loading...

Share This Page