C++ 中的事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/238738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Events in C++
提问by Robert
I'm not sure how to look for this online... I think they might be called something different in C++
我不知道如何在网上寻找这个...我认为它们在 C++ 中可能被称为不同的东西
I want to have a simple event system, somthing like
我想要一个简单的事件系统,比如
event myCustomEvent;
myCustomEvent.subscribe( void myHandler(string) );
myCustomEvent.fire("a custom argument");
// myHandler prints out the string passed in the first argument
event myNewCustomEvent;
myNewCustomEvent.subscribe( void myNewHandler(int) );
myNewCustomEvent.fire(10);
// myHandler prints 10
I can do this pretty easily with a simple class -- but when i want to have an event that passes a different type or amount of arguments to the subscriber i have to write, and define an entirely new event class.. I figure there has to be some library, or maybe even something native in Visual C++ 2008 that will work something similar to this. It's basicly just an implementation of the Observer pattern, so it can't be too impossible to do in C++
我可以用一个简单的类很容易地做到这一点——但是当我想要一个将不同类型或数量的参数传递给订阅者的事件时,我必须编写,并定义一个全新的事件类..我想有成为一些库,或者甚至可能是 Visual C++ 2008 中的本机,它们将与此类似。基本上只是观察者模式的一个实现,所以用C++做也不是不可能
This really makes me appreciate how nice it is in JavaScript not to have to worry about the arguments you are passing.
这真的让我感激在 JavaScript 中不必担心传递的参数是多么美妙。
Tell me if this is a stupid question.
告诉我这是否是一个愚蠢的问题。
回答by Jere.Jones
回答by Keith Nicholas
The observer pattern from the GOF is pretty much what you want.
GOF 的观察者模式几乎就是你想要的。
In the book, it has C++ code for this...
在书中,它有 C++ 代码用于...
Also, as always, Boost has stuff you can make use of as well
此外,与往常一样,Boost 也有你可以使用的东西
回答by Eclipse
There is a native Visual C++ event system. It's mostly for COM, but it has native C++ support too.
有一个本机 Visual C++事件系统。它主要用于 COM,但它也具有本机 C++ 支持。
From here:
从这里:
[event_source(native)]
class CSource {
public:
__event void MyEvent(int nValue);
};
[event_receiver(native)]
class CReceiver {
public:
void MyHandler1(int nValue) {
printf_s("MyHandler1 was called with value %d.\n", nValue);
}
void MyHandler2(int nValue) {
printf_s("MyHandler2 was called with value %d.\n", nValue);
}
void hookEvent(CSource* pSource) {
__hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler1);
__hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler2);
}
void unhookEvent(CSource* pSource) {
__unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler1);
__unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler2);
}
};
int main() {
CSource source;
CReceiver receiver;
receiver.hookEvent(&source);
__raise source.MyEvent(123);
receiver.unhookEvent(&source);
}
回答by Eclipse
I use libsigc++. It's native for gtkmm.
我使用libsigc++。它是 gtkmm 原生的。
A simple example losely adapted from the tutorial:
一个简单的例子,根据教程改编:
#include <iostream>
#include <sigc++/sigc++.h>
using namespace std;
class AlienDetector {
public:
void run ();
sigc::signal<void> 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 而不是 sigc::ptr_fun 将特定对象的成员函数连接到信号的机制:
sigc::mem_fun (someobject, &SomeClass::some_method);
This pretty much provides anything that is possible with GLib-signals.
这几乎提供了 GLib 信号可能提供的任何东西。