什么是订阅事件的C ++ / CLI语法? [英] what is the C++/CLI syntax to subscribe for events?

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

问题描述

我要更新一些旧的托管C ++代码,如下所示:

I'm updating some old Managed C++ code with lines like this:

instanceOfEventSource->add_OnMyEvent( 
    new EventSource::MyEventHandlerDelegate(this, MyEventHandlerMethod) );

其中


  • EventSource 是发布事件的类

  • instanceOfEventSource li> EventSource :: MyEventHandlerDelegate 是事件的委托类型

  • MyEventHandlerMethod 是当前类(其中this是一个实例),签名匹配EventSource :: MyEventHandlerDelegate

  • EventSource is the class that publishes events
  • instanceOfEventSource is an instance of that class
  • EventSource::MyEventHandlerDelegate is the delegate type for the event
  • MyEventHandlerMethod is a (non-static) method within the current class (of which "this" is an instance) with the signature matching EventSource::MyEventHandlerDelegate

C ++ / CLI?

What is the right syntax for this in C++/CLI?

推荐答案

语法与C#类似,换句话说, + = 被重载以使这成为可能:

The syntax is similar to C#'s, in other words, += is overloaded to make this possible:

instanceOfEventSource.MyEvent +=
    gcnew EventSource::MyEventHandlerDelegate(this, &MyClass::MyEventHandlerMethod);

类似删除。然而,与C#不同,您可能不会省略事件处理程序委托的显式实例化,因此这会产生相当长的代码。

Analogously for removal. Unlike C#, however, you may not omit the explicit instantiation of the event handler delegate so this produces quite long-winded code.

这篇关于什么是订阅事件的C ++ / CLI语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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