Multicast.cpp
上传用户:sz0451
上传日期:2022-07-29
资源大小:256k
文件大小:2k
- // This is the main project file for VC++ application project
- // generated using an Application Wizard.
- #include "stdafx.h"
- #using <mscorlib.dll>
- using namespace System;
- __delegate void NotifyDelegate(int);
- __gc class Client1
- {
- public:
- static void NotifyFunction1(int n)
- {
- Console::WriteLine("Client1: got value {0}", __box(n));
- }
- };
- __gc class Client2
- {
- public:
- static void NotifyFunction2(int n)
- {
- Console::WriteLine("Client2: got value {0}", __box(n));
- }
- };
- // This is the entry point for this application
- #ifdef _UNICODE
- int wmain(void)
- #else
- int main(void)
- #endif
- {
- Console::WriteLine("Multicast Delegates");
-
- // Declare two delegates
- NotifyDelegate *pDel1, *pDel2;
- // Bind them to the functions
- pDel1 = new NotifyDelegate(0, &Client1::NotifyFunction1);
- pDel2 = new NotifyDelegate(0, &Client2::NotifyFunction2);
- // Combine the invocation lists of the two delegates
- NotifyDelegate *pDel3 =
- dynamic_cast<NotifyDelegate*>(Delegate::Combine(pDel1, pDel2));
- // Invoke the multicast delegate
- pDel3->Invoke(3);
- // Create a second multicast delegate
- NotifyDelegate *pDel4 =
- dynamic_cast<NotifyDelegate*>(Delegate::Combine(pDel3, pDel3));
-
- // Invoke the multicast delegate
- Console::WriteLine();
- Console::WriteLine("Second delegate");
- pDel4->Invoke(3);
- // Remove an item
- NotifyDelegate *pDel5 =
- dynamic_cast<NotifyDelegate*>(Delegate::Remove(pDel3, pDel1));
- // Invoke delegate with functions removed
- Console::WriteLine();
- Console::WriteLine("Delegate with functions removed");
- pDel5->Invoke(3);
- return 0;
- }