Calling a function or method by reflection in C/C++

眉间皱痕 提交于 2019-12-31 05:43:07

问题


I am just going through a problem that I haven't before in C/C++, and I have no idea how to solve it. Reflection. I need to call a function or method by a string that was given by the user. Not just this, I also need to give the function or method some parameters and get its result if any.

Imagine the user has typed printSomething.

I need to evaluate "printSomething"(paramA, paramB). Of course, the function or method T printSomething() is defined.

How is the best way I can do it?


回答1:


Use a structure mapping from strings to pointers to functions or methods (member functions).

C++ doesn't provide such a structure; you will have to build it yourself, passing in the name-strings and the pointers. Conversion of parameters and return values to and from strings also needs to be implemented. The language has no conventions or ideas about how this is to be done, so you must specify it.



来源:https://stackoverflow.com/questions/14514194/calling-a-function-or-method-by-reflection-in-c-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!