473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Convert string to code

Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...
Thank you

Nov 1 '05 #1
4 13627
muffinman wrote:
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...


Not possible. Search the web for "interpreter c++".

V
Nov 1 '05 #2

"muffinman" <mu**********@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...
Thank you


This is actually very high level, and C++ does not support this type of
programming. It's called reflection, since it's a feature to let a
programming language sort of "see itself". Also called meta programming for
the same reason.

You can do those things in C# (and Java I think), since the run time code
("MS intermediate language" or "Java bytecode") contains information on the
names of classes and their methods and so on.

When you compile a C++ program you end up with assembly, and all that sort
of information will not be available at run time.

Actually, to some degree, you can use a few of the features of reflection in
C++ if you compile your program with the RTTI switch turned on, but your
compiler must support this. And it does not allow full reflection, it just
allows you to do type checking at run time (Run Time Type Information).

-M
Nov 1 '05 #3
> Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!


What you need is just to write your own C++ compiler to compile the string
into executable code. Good luck!

cheers,
Marcin
Nov 1 '05 #4

"muffinman" <mu**********@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello,
I try to convert a string (char[] or string or CString, etc) into
an executable function.

For example :

char sample[20] = "cout << \"sample\"";
char setColor[30] = "glColor3f(1.0f, 1.0f, 0.0f);"

execute(sample); // my wish is to do this!
execute(setColor); // and also this!

then I can run the string.

I am trying to write a game console without directInput. I want to
execute any strings, without switch or if comparisons...
Thank you


C++ is a compiled language. Meaning tokens (such as cout, glColor3f, 1.0f,
etc...) are converted to something the computer understands then converted
to assembly calls. Which means at the time your program is run there is
nothing to convert from cout to the assembly without compiling.

If you want to run arbitrary code at run time you'll need to interpret it.
An interpreted langauge (such as some implementations of java) don't convert
tokens to machine executable instructions until run time.

There are a number of interpreted langauges that will do what you want
without you writing an interpreter, but c++ isn't one of them.
Nov 1 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
3
by: Maileen | last post by:
Hi, How can we convert string^ to String or to LPCWSTR ? thx, Maileen
3
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a...
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
3
by: Shawn Ferguson | last post by:
Hello All, I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the...
13
by: deepthisoft | last post by:
hai, I want to convert String to int.My coding is like this, rs=stmt.executeQuery("select a from mailtable"); rs.absolute(-1); String id=rs.getString("a"); System.out.println(id);
4
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
3
by: tanishka singh | last post by:
How do you convert string from database into date in asp.net? Dim output = (From tlb In obj.SelectRecordAll_SalesInquiryRegister _ Join tlb1 In LatestPOs On tlb.SIR_OfferNo...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.