C++/Cli中事件对象处理函数的添加与删除
生活随笔
收集整理的這篇文章主要介紹了
C++/Cli中事件对象处理函数的添加与删除
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C++/Cli中的事件對象,其實就是一個棧,+=gcnew中事件,再-=gcnew的委托是依次從棧頂消除的。哪怕是同一個類型的委托。
代碼示例如下:
// eventTest.cpp: 主項目文件。#include "stdafx.h"using namespace System;// C3918_3.cpp
// compile with: /clr /c
using namespace System;public delegate void del();public ref class A {
public:static int i=0;event del^ e {void add(del ^handler ) {d += handler;}void remove(del ^handler) {d -= handler;}void raise() { d();}}del^ d;void f() {i=i+1;Console::WriteLine("hello"+i);}A() {//e = gcnew del(this, &A::f); // C3918// try the following line insteade += gcnew del(this, &A::f);e += gcnew del(this, &A::f);e -= gcnew del(this, &A::f);}
};int main() {A^ a=gcnew A;a->e();Console::ReadLine();
}
總結(jié)
以上是生活随笔為你收集整理的C++/Cli中事件对象处理函数的添加与删除的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 说明是有两个虚拟机的HTML5
- 下一篇: s3c2440移植MQTT