Probably more C than C++, but I'm using Borland Builder (v3), so I have that available to me.
I’m writing an application that reads a script file. The script file might look something like this.
5~add~1 2
10~subtract~5 3
15~multiply~3 4
…
It just breaks down as a “~” delimited file. LineNumber~function~parameters.
Now, I can parse it out just fine and get the line numbers into int variables and the functions into char* or AnsiString or whatever variables and get the parameters parsed out ok. What I need to do now is call the functions by those names.
i.e. I have functions
int add(int x, int y)
{
return (x+y);
};
int subtract(int x, int y)
{
return (x-y);
};
int multiply(int x, int y)
{
return(x*y);
};
int main()
{
char function[20];
int x;
int y;
GetScriptLine(function, &x, &y);
// function now contains the char array “add” and x==1 and y==2
// can I call a function something like this
Execute(function, x, y);
/*where Execute() runs the add function in this case and the subtract function on the next script line and multiply… without a big switch statement or a group of if's? I’ve been reading up on pointers to functions, but I haven’t found any examples of
doing anything quite like this. Even something like the JavaScript::eval() function would be fine.*/
return 0;
}
Of course the program is not quite as simple as common math functions, and will have many functions to call (that's why I don't want to use if's) but if I can accomplish this, the translation will be a snap. Any Ideas?
It's not a problem to have to have the parameters all the same either. I have a facility for doing that if I need it. i.e. Execute(function, &DataList); where DataList is simply a list of strings (I'll do the conversion in the add(), subtract(), multiply()... functions because in the real app, the functions will take considerably different arguments.
Happy New Year by the way.
Thanks for any help,
Dana



LinkBack URL
About LinkBacks


