调用list.clear()时出现运行时错误 我想用C++模拟Windows编程中的C事件驱动程序。

调用list.clear()时出现运行时错误 我想用C++模拟Windows编程中的C事件驱动程序。,c++,windows,list,stl,C++,Windows,List,Stl,我想通过以下方式实现这一点: 使用列表容器保存函数指针 重写+=运算符,使类事件可以添加函数 这样事件+=handlerFuncName 重写()运算符,这样我们就可以调用列表中的函数 事件类别: template<class Sender_Type, class Args_Type> class Event{ private: typedef void(*HFUN)(Sender_Type *, Args_Type); list<HFUN> handle

我想通过以下方式实现这一点:

  • 使用列表容器保存函数指针
  • 重写+=运算符,使类事件可以添加函数 这样事件+=handlerFuncName
  • 重写()运算符,这样我们就可以调用列表中的函数
  • 事件类别:

    template<class Sender_Type, class Args_Type>
    class Event{
    private:
        typedef void(*HFUN)(Sender_Type *, Args_Type);
        list<HFUN> handlers;
    public:
        void operator +=(HFUN handler){
            handlers.push_back(handler);
        }
    
        void operator ()(Sender_Type *sender, Args_Type e){       
            for (list<HFUN>::iterator it = handlers.begin(); it != handlers.end(); it++)
                (*it)(sender, e);
        }
    
        ~Event(){ printf("Release..\n"); }
    };
    
    主要功能:

    void Window_Loaded(Win32Window *sender, EventArgs e){
        MessageBox(NULL, "Window_Loaded", "xx", 0);
    }
    
    Win32Window wnd;
    //add the event handler function
    wnd.Loaded += Window_Loaded;
    
    它可以工作,但当窗口关闭时,
    list.clear()中出现运行时错误。

    这里有一个例外:

    我认为这正是
    std::function
    想要帮助解决的问题。对于初学者,可以尝试以下方法:

    #include <functional>
    #include <iostream>
    #include <vector>
    
    template <typename Sender, typename... Args>
    class Event
    {
        using FuncType = std::function<void(Sender&, Args...)>;
        std::vector<FuncType> vec;
    
    public:
        Event& operator+=(FuncType f) {
            vec.push_back(f);
            return *this;
        }
    
        void operator()(Sender& s, Args... a) {
            for (auto& f : vec) {
                f(s, a...);
            }
        }
    };
    
    
    struct Window
    {
        Event<Window, int, int> resized;
    
        void resize(int width, int height) {
            // blah blah
            resized(*this, width, height);
        }
    
    };
    
    int main()
    {
        Window w;
    
        w.resized += [](Window&, int w, int h) {
            std::cout << "Window resized to " << w << " by " << h << std::endl;
        };
    
        w.resize(800, 600);
    }
    
    #包括
    #包括
    #包括
    模板
    班级活动
    {
    使用FuncType=std::function;
    std::vec;
    公众:
    事件和运算符+=(函数类型f){
    向量推回(f);
    归还*这个;
    }
    void运算符(){
    用于(自动&f:vec){
    f(s,a…);
    }
    }
    };
    结构窗口
    {
    事件大小调整;
    空心调整大小(整型宽度、整型高度){
    //废话
    调整大小(*此,宽度,高度);
    }
    };
    int main()
    {
    窗口w;
    w、 调整大小+=[](窗口和,整数w,整数h){
    
    std::cout甚至看不到您的图像…什么是HFUN?您能显示初始化和清除此列表的代码吗,事件处理程序代码的问题是不必要的。请尝试将其简化为请。成员函数指针显然不是答案。请尝试阅读Functors.typedef void(*HFUN)(Sender_Type*,Args_Type);我没有调用clear()函数。我想它是在列表发布时自动调用的。列表(处理程序)的所有操作都显示在这些代码中,不再对其执行任何操作。使用
    std::function
    肯定会使其更易于阅读..并且更安全可靠。这对我很有帮助。
    #include <functional>
    #include <iostream>
    #include <vector>
    
    template <typename Sender, typename... Args>
    class Event
    {
        using FuncType = std::function<void(Sender&, Args...)>;
        std::vector<FuncType> vec;
    
    public:
        Event& operator+=(FuncType f) {
            vec.push_back(f);
            return *this;
        }
    
        void operator()(Sender& s, Args... a) {
            for (auto& f : vec) {
                f(s, a...);
            }
        }
    };
    
    
    struct Window
    {
        Event<Window, int, int> resized;
    
        void resize(int width, int height) {
            // blah blah
            resized(*this, width, height);
        }
    
    };
    
    int main()
    {
        Window w;
    
        w.resized += [](Window&, int w, int h) {
            std::cout << "Window resized to " << w << " by " << h << std::endl;
        };
    
        w.resize(800, 600);
    }