Last Updated:

Delegates in c++

The _delegate (delegate) keyword is used to declare a delegate class based on the description of the method signature. Delegates are very similar to function pointers in regular C++, with the only difference being that a delegate can only point to a method of a managed class. Most commonly, .NET Framework applications use delegates to implement callback functions or event handling. However, they can be used in all cases where you need to call methods dynamically.

The .NET Framework defines (as abstract classes) two types of delegates, System - D elegate and System - MulticastDelegate. These two types of delegates are used as base classes for unicast (or single-cast delegates) and multicast (or multicast) delegates, respectively. A unicast delegate associates a method pointer with a method of a single managed object, whereas a multicast delegate binds a method pointer to one or more methods of a managed object. Calling a unicast delegate calls only one method, and calling a multicast delegate can execute an unlimited number of methods. Because a multicast delegate can also be used to call a single method, the delegate's unicast form is redundant. Typically, programs use only multicast delegates.

When the compiler encounters the _delegate (delegate) keyword in the program, it creates a special managed class that derives from System - MulticastDelegate. The constructor of this class has two arguments: a pointer to an instance of the managed class (which is zero if the delegate binds a static method), and the method itself, which is called by a delegate. This class also contains an Invoke method whose signature matches the signature of the method called by the delegate. The following example demonstrates the use of delegates:

//DelegateExample.cpp
#using <mscorlib.dll>
using namespace System;
// use namespace System;
// define managed classes to use
// as delegates
_delegate int SomeDelegate // delegate
(int i, int j);
_delegate // delegate
void SomeOtherDelegate(int i);
_gc class SomeClass
// garbage collector class SomeClass contains methods,
// called by delegates
{
public:
int SomeMethod(int i, int j)
{
Console::WriteLine(
"SomeMethod({0}, {!})", _box(i), _box(j));
return i+j; }
static int SomeStaticMethod(int i, int j) // static
{
Console::WriteLine(
"SomeStaticMethod({0}, {!})", _box(i), _box(j));
return i+j; }
void SomeOtherMethod(int i) {
Console::WriteLine(
11 SomeOtherMethod ({0}) ", _box(i));
}
};
void main()
{
SomeDelegate *pscd; intsum; // sum
// bind a delegate to a non-static method
// instance required
SomeClass SomeClass * psc = newSomeClass();
pscd = // instantiate delegate sc new SomeDelegate(
psc, SSomeClass::SomeMethod); // non-static
sum = pscd › Invoke(3, 4);
// call method via delegate
// sum = pscd › call (3, 4);
Console::WriteLine(sum); // sum
// bind delegate to static method - no need
// not in any instance
pscd = // create another instance of the delegate class
sc new SomeDelegate((), SSomeClass::SomeStaticMethod);
// static sum = pscd › Invoke(3, 4);
// call method via delegate
// sum = pscd › call (3, 4);
Console::WriteLine(sum); // sum
// concatenate two delegates
SomeClass * pscl = new SomeClass();
SomeClass * psc2 = new SomeClass(); SomeOtherDelegate *pmcdl = new SomeOtherDelegate(
psc1, &SomeClass::SomeOtherMethod); SomeOtherDelegate *pmcd2 = new SomeOtherDelegate(
psc2, SSomeClass::SomeOtherMethod); SomeOtherDelegate *pmcd =
static_cast<SomeOtherDelegate *>(Delegate:.Combine(
// Combining delegates pmcdl, pmcd2)); pmcd › Invoke(1); // Call }
SomeMethod(3, 4)
7
SomeStaticMethod(3, 4)
7
SomeOtherMethod(I)
SomeOtherMethod(1)