I've got an implemented function MyFunc(int, double, string)
in my project. How can I call this function with necessary parameters having its string representation, for example
std::string str = "MyFunc(2, 3.12, \"Lemon Juice\")";
And what about the standard functions, for example, how can I call time()
having std::string "time()"
?
Ok, here's more detailed task.
I've got a map <string, Data>
Data
is a class-wrapper with many childs. When I call Data.GetValue()
method, it returns a std::string
, depending of child class inner data. One of Data
childs must return the string representation of int
, another - the string representation of double
, and the last one - the string representation of current date and time. Here's the problem - I don't know how to call the standard function ctime()
for getting information from one of Data
childs.
You cannot in general execute code in C++ whose source is contained in a string. Probably the most compelling motivation for this is so that a C++ implementation is not required to have a C++ compiler on the machine that the code runs on, although there is more to it than just that.
You may be able to do it for specific cases. That could be:
Platform-specific, for example:
Input-specific, for example:
It sounds as though in your task you have a string that is one of:
int
double
You don't say what you want to do with this string, but whatever that is you probably need to (a) examine the string to find out which of the three it is, and then (b) do something appropriate to that format. Better, you could give the derived class the responsibility of returning the same representation no matter which of the three GetValue()
returns. For example, if what you really want is seconds since 1970, add a GetSecondsSinceEpoc
function, and implement it differently in each class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With