You are here:

C++/eval function from javascript in C++?


is there something like the eval function from javascript in C++?

i remember dynamically creating variables in javascript using the eval function, i need to dynamically create variables in C++.
wait, or maybe i can just use string arrays, haha... why didn't I think of that?
oh well.
well if you do know, you don't have to give me any detailed explanations of it, just tell me the function and ill look it up in the c++ manual.

thank you very much

In this respect C++ is fundamentally different from Javascript, Java, or the .NET languages. It is a statically typed and compiled system - that means that all types, variables and the like are known to the compiler during compilation, and resolved during linking before an executable is produced.

You cannot dynamically create strings of code and have them executed at runtime. OK, if you wish to be picky you could write out a C++ source code file, execute the compiler to compile it, execute the linker to link it with the required libraries and produce a new executable, and then execute this executable, but I do not think this is what you want.

You have touched on a solution using string as the variable names. However note that even if you have the so-called variable as a string that it is still not something the program will know about. You will have to add the code to process and manipulate these variables. For example if you have the following:

       "A=10; B=20; C=A+B"

You will have write all the code to process the string, associate "A" with "10" or 10 or 10.0 (string, integer or floating point type), B with "20" or 10 or 20.0 and C with "A+B" or 30 or 30.0 or maybe "30". To actually evaluate C=A+B you would have to write an expression parser and evaluator. So what you will end up with is your own little script or macro language.

On a practical note you might wish to associate the values of your script variables with their names using a map - the standard library has such a thing - in fact it is a class template - and is called std::map, and you should include the standard C++ header <map>. A map is an associative type - it associates a key with a value. In this case the key is the variable name and the value is the variable value.

If you have not come across the C++ standard library you might like to check out the online information at which covers a large part of it - specifically the things such as std::map. If your C++ development system does not come with a C++ standard library then check out for a freely downloadable and usable version that can be used with a wide variety of compilers.

For further information I suggest some books.

For learning C++ coming from another language try "Accelerated C++" by Koenig and Moo. I have not read it as I found out about it far too late, but I have seen it recommended for people such as yourself many times.

For a standard C++ library reference and tutorial I recommend "The C++ Standard Library - a tutorial and reference" by Nicolai M. Josuttis - my copy is one of my most referred to C++ books.

Another book by Koenig and Moo (which I have read!) has a nice example of dealing with arithmetic expressions in an object oriented way using C++ - the book is called "Rumination on C++" (see chapter 8 in particular). I am sure this is not only book to contain such an example. However the rest of this book is very good and provided me with much food for thought.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at


©2017 All rights reserved.