Events in C++

后端 未结 5 891
遇见更好的自我
遇见更好的自我 2021-01-31 10:32

I\'m not sure how to look for this online... I think they might be called something different in C++

I want to have a simple event system, somthing like

         


        
5条回答
  •  鱼传尺愫
    2021-01-31 10:51

    I use libsigc++. It's native for gtkmm.

    A simple example losely adapted from the tutorial:

    #include 
    #include 
    
    using namespace std;
    
    class AlienDetector {
    public:
            void run ();
            sigc::signal signal_detected;
    };
    
    void warn_people () {
            cout << "There are aliens in the carpark!" << endl;
    }
    
    void AlienDetector::run () {
            signal_detected.emit ();
    }
    
    int main () {
            AlienDetector mydetector;
            mydetector.signal_detected.connect (sigc::ptr_fun (warn_people));
            mydetector.run ();
    }
    

    It also provides a mechanism to connect member-functions of specific objects to signals using sigc::mem_fun instead of sigc::ptr_fun:

    sigc::mem_fun (someobject, &SomeClass::some_method);
    

    This pretty much provides anything that is possible with GLib-signals.

提交回复
热议问题