description | title | ms.date | ms.topic | f1_keywords | helpviewer_keywords | ms.assetid | |||
---|---|---|---|---|---|---|---|---|---|
Learn more about: delegate (C++/CLI and C++/CX) | delegate (C++/CLI and C++/CX) | 10/12/2018 | reference |
|
| 03caf23d-7873-4a23-9b34-becf42aaf429 |
Declares a type that represents a function pointer.
Both the Windows Runtime and common language runtime support delegates.
delegate is a context-sensitive keyword. For more information, see Context-Sensitive Keywords.
To detect at compile time if a type is a delegate, use the __is_delegate()
type trait. For more information, see Compiler Support for Type Traits.
C++/CX supports delegates with the following syntax.
access delegate return-type delegate-type-identifier ( [ parameters ] )
access
(optional) The accessibility of the delegate, which can be public
(the default) or private
. The function prototype can also be qualified with the const
or volatile
keywords.
return-type
The return type of the function prototype.
delegate-type-identifier
The name of the declared delegate type.
parameters
(Optional) The types and identifiers of the function prototype.
Use the delegate-type-identifier to declare an event with the same prototype as the delegate. For more information, see Delegates (C++/CX).
Compiler option: /ZW
The common language runtime supports delegates with the following syntax.
access delegate function_declaration
access
(optional) The accessibility of the delegate outside of the assembly can be public or private. The default is private. Inside a class, a delegate can have any accessibility.
function_declaration
The signature of the function that can be bound to the delegate. The return type of a delegate can be any managed type. For interoperability reasons, it is recommended that the return type of a delegate be a CLS type.
To define an unbound delegate, the first parameter in function_declaration should be the type of the this
pointer for the object.
Delegates are multicast: 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:
It inherits from
System::MulticastDelegate
.It has a constructor that takes two arguments: a pointer to a managed class or NULL (in the case of binding to a static method) and a fully qualified method of the specified type.
It has a method called
Invoke
, whose signature matches the declared signature of the delegate.
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.
Delegates 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).
For more information on delegates, see
Compiler option: /clr
The following example shows how to declare, initialize, and invoke delegates.
// mcppv2_delegate.cpp// compile with: /clrusingnamespaceSystem;// declare a delegate public delegate voidMyDel(int i); ref classA { public:voidfunc1(int i) { Console::WriteLine("in func1 {0}", i); } voidfunc2(int i) { Console::WriteLine("in func2 {0}", i); } staticvoidfunc3(int i) { Console::WriteLine("in static func3 {0}", i); } }; intmain () { A ^ a = gcnew A; // declare a delegate instance MyDel^ DelInst; // test if delegate is initializedif (DelInst) DelInst(7); // assigning to delegate DelInst = gcnew MyDel(a, &A::func1); // invoke delegateif (DelInst) DelInst(8); // add a function DelInst += gcnew MyDel(a, &A::func2); DelInst(9); // remove a function DelInst -= gcnew MyDel(a, &A::func1); // invoke delegate with Invoke DelInst->Invoke(10); // make delegate to static function MyDel ^ StaticDelInst = gcnew MyDel(&A::func3); StaticDelInst(11); }
in func1 8 in func1 9 in func2 9 in func2 10 in static func3 11