The code snippets used in this module if any are Visual C++ .Net 2003 dominated and if compiled using Visual C++ .Net 2005 you need to use the /clr:oldSyntax option). The following are the topics available in this module.
| The new delegate Keyword
Keyword delegate defines a reference type that can encapsulate one or more methods with a specific function prototype. Delegates provide the underlying mechanism (acting as a kind of pointer to member function) for events in the common language runtime component model. It is a new keyword that replacing the __delegate. The syntax is:
Delegates are multicast, that means the "function pointer" can be bound to one or more methods within a managed class. The delegate keyword defines a multicast delegate type with a specific method signature. A delegate can also be bound to a method of a value class, such as a static method. A delegate has the following characteristics:
When a delegate is invoked, its function(s) are called in the order they were attached. The return value of a delegate is the return value from its last attached member function. Delegates cannot be overloaded and it is a context-sensitive keyword and can be bound or unbound. When you instantiate a bound delegate, the first argument shall be an object reference. The second argument of a delegate instantiation shall either be the address of a method of a managed class object, or a pointer to a method of a value type. The second argument of a delegate instantiation | |||||||||
must name the method with the full class scope syntax and apply the address-of operator (&). When you instantiate an unbound delegate, the first argument shall either be the address of a method of a managed class object, or a pointer to a method of a value type. The argument must name the method with the full class scope syntax and apply the address-of operator. When creating a delegate to a static or global function, only one parameter is required: the function (optionally, the address of the function). You can detect at compile time if a type is a delegate with __is_delegate (type). The following example shows how to declare, initialize, and invoke delegates.
// Test.cpp : main project file.
// delegate keyword
// compile with: /clr
#include "stdafx.h"
using namespace System;
// declare a delegate
public delegate void MyDel(int i);
ref class myA
{
public:
void func1(int i)
{
Console::WriteLine("In func1, i = {0}", i);
}
void func2(int i)
{
Console::WriteLine("In func2, i = {0}", i);
}
static void func3(int i)
{
Console::WriteLine("In static func3, i = {0}", i);
}
};
int main ()
{
myA ^ mya = gcnew myA;
// declare a delegate instance
MyDel^ DelInst;
// test if delegate is initialized
Console::WriteLine("...delegate is initialized...");
if (DelInst)
DelInst(7);
// assigning to delegate
DelInst = gcnew MyDel(mya, &myA::func1);
// invoke delegate
Console::WriteLine("\n...Invoking delegate...");
if (DelInst)
DelInst(8);
// add a function
Console::WriteLine("\n...Adding a function...");
DelInst += gcnew MyDel(mya, &myA::func2);
DelInst(9);
// remove a function
DelInst -= gcnew MyDel(mya, &myA::func1);
// invoke delegate with Invoke
Console::WriteLine("\n...Invoking delegate with Invoke...");
DelInst->Invoke(10);
// make delegate to static function
Console::WriteLine("\n...Making delegate to static function...");
MyDel ^ StaticDelInst = gcnew MyDel(&myA::func3);
StaticDelInst(11);
return 0;
}
Output:
![]() |
Event Handling
Event handling is primarily supported for COM classes (C++ classes that implement COM objects, typically using ATL classes or the coclass attribute). Event handling is also supported for native C++ classes (C++ classes that do not implement COM objects), however, that support is deprecated and will be removed in a future release. Event handling supports single- and multithreaded usage and protects data from simultaneous multithread access. It also allows you to derive subclasses from event source or receiver classes and support extended event sourcing/receiving in the derived class. Visual C++ includes attributes and keywords shown in the following Table for declaring events and event handlers. The event attributes and keywords can be used in CLR programs and in native C++ programs.
| Attribute & Keyword | Description |
| event_source | Creates an event source. |
| event_receiver | Creates an event receiver (sink). |
| __event | Declares an event. |
| __raise | Emphasizes the call site of an event. |
| __hook | Associates a handler method with an event. |
| __unhook | Dissociates a handler method from an event. |
|
Table 6 | |