executing code in a String!

V

v4vijayakumar

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::string code)" function?
 
A

Alf P. Steinbach

* (e-mail address removed):
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::string code)" function?

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

benben

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::string code)" function?

// Just to give an idea:

void execute(std::string str)
{
std::eek:fstream o("temp.cpp");
o << "#include <everything.hpp>"
<< "int main(){" << str << "}";

o.close();

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


Regards,
Ben
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top