NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

__delegate

Defines a reference type that can be used to encapsulate a method with a specific signature.

__delegate function-declarator
__delegate( multicast ) function-declarator

Remarks

A delegate is roughly equivalent to a C++ function pointer except for the following differences:

Multicast delegates (a delegate that points to multiple methods) are also supported by a variant of the __delegate keyword: __delegate( multicast ).

When the compiler encounters the __delegate keyword, a definition of a managed class is generated. This managed class has the following characteristics:

For more information on delegates and their base implementations, see Delegates in Managed Extensions for C++ and Handling Events in NGWS Framework.

Example

In the following example, a managed class (MyCalendar) and a single-cast delegate are declared (GetDayOfWeek). The delegate is then bound to the different methods of MyCalendar, invoking each in turn:

#using <mscorlib.dll>
#using namespace System;

__delegate int GetDayOfWeek();
__gc class MyCalendar
{
public:
   MyCalendar() : m_nDayOfWeek(4) {}
   int MyGetDayOfWeek() { return m_nDayOfWeek; }
   static int MyStaticGetDayOfWeek() { return 6; }
private:
   int m_nDayOfWeek;
};

void main ()
{
   GetDayOfWeek * pGetDayOfWeek;  // declare delegate type
   int nDayOfWeek;
   
   // bind delegate to static method
   pGetDayOfWeek = new GetDayOfWeek(0, MyCalendar::MyStaticGetDayOfWeek);
   nDayOfWeek = pGetDayOfWeek->Invoke();
   Console::WriteLine(dynamic_cast<Object *>(nDayOfWeek)->ToString());

   // bind delegate to instance method
   MyCalendar * pcal = new MyCalendar();
   pGetDayOfWeek = new GetDayOfWeek(pcal, &MyCalendar::MyGetDayOfWeek);
   nDayOfWeek = pGetDayOfWeek->Invoke();
   Console::WriteLine(dynamic_cast<Object *>(nDayOfWeek)->ToString());
}

See Also

Managed Extensions for C++ Keywords | C++ Keywords