简体   繁体   English

如何将成员 function 作为参数传递?

[英]How to pass a member function as an argument?

I have the following Classes:我有以下课程:

typedef void (*ScriptFunction)(void);
typedef std::unordered_map<std::string, std::vector<ScriptFunction>> Script_map;

class EventManager
{
public:

    Script_map subscriptions;

    void subscribe(std::string event_type, ScriptFunction handler);
    void publish(std::string event);

};

class DataStorage
{
    std::vector<std::string> data;
public:

    EventManager &em;

    DataStorage(EventManager& em);
    void load(std::string);
    void produce_words();


};
DataStorage::DataStorage(EventManager& em) : em(em) {
    this->em.subscribe("load", this->load);     
};

I want to be able to pass DataStorage::load to EventManager::subscribe so i can call it later on.我希望能够将 DataStorage::load 传递给 EventManager::subscribe,以便稍后调用它。 How can i achieve this in c++?我怎样才能在 c++ 中实现这一点?

The best way to do this would be with an std::function :最好的方法是使用std::function

#include <functional>

typedef std::function<void(std::string)> myFunction;
// Actually, you could and technically probably should use "using" here, but just to follow
// your formatting here

Then, to accept a function, you need simply need to do the same thing as before:然后,要接受 function,您只需要像以前一样做同样的事情:

void subscribe(std::string event_type, myFunction handler);
// btw: could just as easily be called ScriptFunction I suppose

Now the tricky part;现在是棘手的部分; to pass a member function, you actually need to bind an instance of DataStorage to the member function.要传递成员 function,实际上需要将DataStorage的实例绑定到成员 function。 That would look something like this:看起来像这样:

DataStorage myDataStorage;
EventManager manager;

manager.subscribe("some event type", std::bind(&DataStorage::load, &myDataStorage));

Or, if you're inside a member function of DataStorage :或者,如果您在DataStorage的成员 function 中:

manager.subscribe("some event type", std::bind(&DataStorage::load, this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递成员函数作为参数 - Pass member function as argument 如何将私有成员函数作为参数传递 - How to pass a private member function as an argument 如何将带有args的成员函数作为参数传递给另一个成员函数? - how to pass a member function with args as an argument to another member function? 如何将lamda作为成员函数的函数指针参数传递? - How to pass lamda as a function pointer argument of member function? 如何传递具有可变参数作为模板参数的成员函数? - How to pass a member function which has variable arguments as template argument? 在这种情况下,如何在C ++中正确传递成员函数作为参数? - How to properly pass member function as argument in this situation in C++? 如何转换变量成员以将其作为函数的引用参数传递 - How to cast a variable member to pass it as reference argument of a function 如何将非静态成员函数传递给模板参数? - how to pass non-static member function to template argument? 传递一个对象成员函数作为参数(函数指针) - Pass an objects member function as argument (function pointer) 如何将一个 class 的成员 function 作为参数传递给另一个 ZA2F2ED4F8EBC2CBBD4ZC2 的成员 function? - How can I pass member function of one class as an argument to member function of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM