C ++中的事件 [英] Events in C++

查看:51
本文介绍了C ++中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何在网上查找此内容...我认为它们在C ++中可能会被称为

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

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

我可以用一个简单的类很容易地做到这一点-但是当我想要一个将不同类型或数量的参数传递给订阅者的事件时,我必须编写并定义一个全新的事件类.图中必须有一些库,甚至可能是Visual C ++ 2008中的本机库,其工作方式也与此类似.从根本上讲,它只是Observer模式的一种实现,因此在C ++中做到这一点并非不可能

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++

这真的让我意识到不用担心传递的参数对JavaScript的好处.

This really makes me appreciate how nice it is in JavaScript not to have to worry about the arguments you are passing.

如果这是一个愚蠢的问题,请告诉我.

Tell me if this is a stupid question.

推荐答案

我正是出于这个目的使用 sigslot

I use sigslot for exactly this purpose.

这篇关于C ++中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆