-3

I want to call C++ functions on this way:

I have an array that matches strings with corresponding c++ function

int x() {
return 6;
}

int y(int t){

return t;
}

A functions[] = { { "x" , void*&x } , { "y", void*&y}};

A is as simple struct. The program gets a string as input of user.

If i have a black-magic box function Z , that takes as parameter a string, and searchs in array "functions" and returns the second element of struct which equals with the string parameter. For example Z("x") returns void*&x ..etc..

If i don't know all the functions in functions array how can i call the returned function of Z?

for example i want something like this:

void *  g = Z("y");   //returns the y function
g(8);

I don't want a specific cast or if/else, because for the first the first time the user maybe give "x" and the seconde time give "y", and for the second I don't want if/else cases because the number of function may be infinity.

phuclv
  • 37,963
  • 15
  • 156
  • 475
LeAdErQ
  • 45
  • 10
  • Will all the functions have the same signature (the same type of return value and input parameters)? – Sami Sallinen Jan 16 '18 at 06:31
  • As in the example I mentioned x and y functions have different signature. So the answer is no. – LeAdErQ Jan 16 '18 at 06:35
  • How do you retrieve the arguments that you will use to call the function (e.g. the `8` in your example)? – Holt Jan 16 '18 at 06:51
  • I don't know, for this reason I asked the question. if there is a way that passes the parameters to the second element for each element of the table .. something like this : { "y" , void*&y(int)} – LeAdErQ Jan 16 '18 at 06:56
  • 3
    @LeAdErQ You asked how to *call* the functions, but you have to retrieve the arguments somewhere... Like where does the `8` come from? Maybe you should expand on the original problem you're trying to solve? – Holt Jan 16 '18 at 06:58
  • 1
    Well, if you say that to Holt, then *I* will tell you, we don't really owe you an answer, and it's up to *you* to present it clearly. Nobody is going to trip over themselves helping someone rude. – StoryTeller - Unslander Monica Jan 16 '18 at 07:03
  • @StoryTeller the requested information does not help solve the problem. The problem is specific. – LeAdErQ Jan 16 '18 at 07:06
  • 2
    You have no idea what may help solve the problem. If you did, you wouldn't have the problem. – StoryTeller - Unslander Monica Jan 16 '18 at 07:07
  • These belong to the category "philosophies". Have a nice day. – LeAdErQ Jan 16 '18 at 07:08
  • I don't need philosophy to judge a bad question and a terrible attitude. There's an objective standard for those – StoryTeller - Unslander Monica Jan 16 '18 at 07:11
  • It is different to ask something that I do not specify in the question, than to ask why I want it – LeAdErQ Jan 16 '18 at 07:15
  • Look into how to write a parser. Looking up terms like tokenizing, lexing and abstract syntax tree will also be helpful. – Jesper Juhl Jan 16 '18 at 07:17
  • I have read all this. If it is impossible that just you can tell me. – LeAdErQ Jan 16 '18 at 07:19
  • Have a look at [this](https://stackoverflow.com/a/45718187/2610810). You *will* need to know which parameter types apply to which entry in the map – Caleth Jan 16 '18 at 13:39
  • Possible duplicate of [Store functions with different signatures in a map](https://stackoverflow.com/questions/45715219/store-functions-with-different-signatures-in-a-map) – Caleth Jan 16 '18 at 13:41

2 Answers2

0

Let's look at the functions array

A functions[] = { { "x" , void*&x } , { "y", void*&y}};

It looks to me like each function gets stored along with their name. In order to find the function with a given name, it's as simple as finding the A instance that stores that name. You can do this using a for loop to iterate over the functions array.

for(size_t i = 0; i < sizeof(functions) / sizeof(A); i++) {
    if(functions[i].name == desiredFunctionName) {
        return functions[i].fxn;
    }
}
EKW
  • 2,059
  • 14
  • 24
0

Create a map where key value is a string e.g.- "x", "Y" and value is Function Pointer. When you will search with key value in map then you will get function pointer to desired function. Assign it to void * g and then dereference it to call that function.

Rahul Dabas
  • 1
  • 1
  • 1