say have following class: (possibly meta-generated)
class myclass { public: mymethod(); ... } assuming few things here:
1.) have class name somewhere (let's pretend) 2.) have names of class methods somewhere ( std::map< std::string, std::function> ... perhaps? ) so...since may not know name of mymethod() until runtime, there way call using std::string ? assuming have names of class functions stored somewhere.
myclass example; std::string funcname{ findmymethod() };//get std::string name of mymethod example.somehowrunmymethodusing_funcname_(); i know c++ not suited introspection-like situations, i'd figure out.
thanks!
there many ways, using map of member function pointers among general methods same signature.
#include <iostream> #include <map> #include <string> using namespace std; class my_class { public: void method_1() { wcout << "method_1\n"; } void method_2() { wcout << "method_2\n"; } void method_3() { wcout << "method_3\n"; } }; auto method_name() -> string { return "method_3"; } auto main() -> int { map<string, void (my_class::*)()> methods = { { "method_1", &my_class::method_1 }, { "method_2", &my_class::method_2 }, { "method_3", &my_class::method_3 }, }; my_class example; (example.*methods.at( method_name() ))(); } supporting different signatures harder.
then diy runtime type checking.
Comments
Post a Comment