executing code in a String!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • v4vijayakumar@yahoo.com

    executing code in a String!

    How i can execute c++ code in a String?

    for example,
    string code = "cout << \"hi\" << endl;";
    execute(code);

    How i can implement that "execute(std::s tring code)" function?

  • Alf P. Steinbach

    #2
    Re: executing code in a String!

    * v4vijayakumar@y ahoo.com:[color=blue]
    > How i can execute c++ code in a String?
    >
    > for example,
    > string code = "cout << \"hi\" << endl;";
    > execute(code);
    >
    > How i can implement that "execute(std::s tring code)" function?[/color]

    There's no such functionality provided by the language or its standard
    library.

    But you could a C++ interpreter, provided it has some 'eval' hook.

    Google for that (or, as I recall Wikipedia had an informative page).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • benben

      #3
      Re: executing code in a String!

      v4vijayakumar@y ahoo.com wrote:[color=blue]
      > How i can execute c++ code in a String?
      >
      > for example,
      > string code = "cout << \"hi\" << endl;";
      > execute(code);
      >
      > How i can implement that "execute(std::s tring code)" function?
      >[/color]

      // Just to give an idea:

      void execute(std::st ring str)
      {
      std::ofstream o("temp.cpp") ;
      o << "#include <everything.hpp >"
      << "int main(){" << str << "}";

      o.close();

      system("g++ temp.cpp");
      system("a.out") ;
      }


      Regards,
      Ben

      Comment

      Working...