Search code examples
c++function-pointers

C++ runtime member function access by string name


I have two classes:

class MyClassInfo {
public:
  void AddMethod(std::string name, void* pointer); // I don't know what signature should be
}
class MyClass
{
public: 
  void SetField1(int f1);
  int GetFileld1();
private:
  int field1;
};

I need to be able to access the methods of MyClass by name (string) during runtime. I don't want to use any libraries (except boost) and/or compiler functionality (such as rtti) to do that.

I don't know which signature I should use in AddMethod method, because I don't know how to transmit the function pointer to the function. This function must be a universal function which allows to add any method. Maybe you know a better variant how to do that without MyClassInfo. Any help will be appreciated.


Solution

  • That is not possible in directly C++. You will have to look at alternatives like creating a map of names to member function pointers. But even that would require consistent method signatures.

    Something like

    std::map<std::string, int (MyClass::*)(void*))
    

    for instance. Your AddMethod signature would then look like this

    void AddMethod(std::string name, int (MyClass::* pointer)(void*));
    

    and you would call it like this

    info.AddMethod("someMethod", &MyClass::someMethod);