Calling a Function From a String With the Function’s Name in C++

前端 未结 8 1089
故里飘歌
故里飘歌 2020-12-10 15:35

How can I call a C++ function from a string?

Instead of doing this, call the method straight from string:

void callfunction(const char* callthis, int         


        
相关标签:
8条回答
  • 2020-12-10 16:31

    Alternative: You could use array of function pointers and call the desired function using its index.

    typedef void (*TFunc)(int, int);
    
    TFunc arrptr[100];
    
    void callFunction(int index, int params[])
    {
        (*arrptr[index])(params[0], params[1]);
    }
    
    0 讨论(0)
  • 2020-12-10 16:32

    You could wrap your function declarations into an interpreted language (like Lua or Perl or Python (boost has some nice framework for that) ). Then use that language to call your code 'by string'.

    These languages/wrappers are built to do such things. C++ isn't, so you will put a lot of effort in adding support for it.

    0 讨论(0)
提交回复
热议问题