By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

Convert string to code

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
> 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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.