You could create a Handlerdelegate like this:
Handler^ handler = gcnew Handler(HandlerClass::Fun1); // Delegate object
The handler object contains the address of the static function, Fun1, in the HandlerClassclass. If you call the delegate, the HandlerClass::Fun1()function is called with the argument the same as you pass in the delegate call. You can write the delegate call like this:
handler->Invoke(90);
This calls all the functions in the invocation list for the handler delegate. In this case there is just one function in the invocation list, HandlerClass::Fun1(), so the output is:
Function1 called with value 90
You could also call the delegate with the following statement:
handler(90);
This is shorthand for the previous statement that explicitly called the Invoke()function and this is the form of delegate call you see generally.
The + operator is overloaded for delegate types to combine the invocation lists for two delegates into a new delegate object. For example, you could apparently modify the invocation list for the handler delegate with this statement:
handler += gcnew Handler(HandlerClass::Fun2);
The handler variable now references a delegate object with an invocation list containing two functions: Fun1 and Fun2. However, this is a new delegate object. The invocation list for a delegate cannot be changed so
Chapter 9: Class Inheritance and Virtual Functions
the + operator works in a similar way to the way it works with Stringobjects - you always get a new object created. You could invoke the delegate again with this statement:
handler(80);
Now you get the output:
Function1 called with value 80 Function2 called with value 80
Both functions in the invocation list are called and they are called in the sequence in which they were added to the delegate object.
You can effectively remove an entry from the invocation list for a delegate by using the - operator:
handler -= gcnew Handler(HandlerClass::Fun1);
This creates a new delegate object that contains just HandlerClass::Fun2()in its invocation list. The effect of using the -= operator is to remove the functions that are in the invocation list on the right side (HandlerClass::Fun1) from the list for the handler and create a new object pointing to the functions that remain.
Note that the invocation list for a delegate must contain at least one function pointer. If you remove
all the function pointers using the subtraction operator then the result will be nullptr.
When you use the delegate constructor that has two parameters, the first argument is a reference to an object on the CLR heap and the second object is the address of an instance function for that object's type. Thus this constructor creates a delegate that contains a pointer to the instance function specified by the second argument for the object specified by the first argument. Here's how you can create such a delegate:
HandlerClass^ obj = gcnew HandlerClass;
Handler^ handler2 = gcnew Handler (obj, &HandlerClass::Fun3);
The first statement creates an object and the second statement creates a delegate pointing to the Fun3() function for the HandlerClassobject obj. The delegate expects an argument of type int so you can invoke it with the statement:
Visual Studio C++ 2008 Part 2
Start from the beginning
