Visual Studio C++ 2008 Part 2

Mulai dari awal
                                        

Delegate with three pointers to functions:"); handler(70);

Console::WriteLine(L"

Shortening the invocation list..."); handler -= gcnew Handler(HandlerClass::Fun1); Console::WriteLine

(L"

Delegate with pointers to one static and one instance function:"); handler(60); }

This example produces the following output:

Delegate with one pointer to a static function:

Function1 called with value 90

Delegate with two pointers to static functions: Function1 called with value 80 Function2 called with value 80

Delegate with three pointers to functions: Function1 called with value 70 Function2 called with value 70 Function3 called with value 71

Shortening the invocation list...

Delegate with pointers to one static and one instance function: Function2 called with value 60 Function3 called with value 61

How It Works

You saw all the operations that appear in main()in the previous section. You invoke a delegate using the Invoke()function explicitly and by just using the delegate handle followed by its argument list. You can see from the output that everything works as it should.

Chapter 9: Class Inheritance and Virtual Functions

Although the example shows a delegate that can contain pointers to functions with a single argument, a delegate can point to functions with as many arguments as you want. For example, you could declare a delegate type like this:

delegate void MyHandler(double x, String^ description);

This statement declares the MyHandlerdelegate type that can only point to functions with a voidreturn type and two parameters, the first of type doubleand the second of type String^.

Unbound Delegates

The delegates you have seen up to now have been examples of bound delegates. They are called bound delegates because they each have a fixed set of functions in their invocation list. You can also create unbound delegates; an unbound delegate points to an instance function with a given parameter list and return type for a given type of object. Thus the same delegate can invoke the instance function for any object of the specified type. Here's an example of declaring an unbound delegate:

public delegate void UBHandler(ThisClass^, int value);

The first argument specifies the type of the thispointer for which a delegate of type UBHandlercan call an instance function; the function must have a single parameter of type intand a return type of void. Thus a delegate of type UBHandlercan only call a function for an object of type ThisClassbut for any object of that type. This may sound a bit restrictive but turns out to be quite useful; you could use the delegate to call a function for each element of type ThisClass^in an array for example.

You can create a delegate of type UBHandlerlike this:

UBHandler^ ubh = gcnew UBHandler(&ThisClass::Sum);

The argument to the constructor is the address of a function in the ThisClassclass that has the required parameter list and return type.

Here's a definition for ThisClass:

public ref class ThisClass { public:

void Sum(int n, String^ str)

{ Console::WriteLine(L"Sum result = {0}", value + n); } void Product(int n, String^ str) { Console::WriteLine(L"Product result = {0}", value*n); } ThisClass(double v) : value(v){} private: double value; };

Kamu telah mencapai bab terakhir yang dipublikasikan.

⏰ Terakhir diperbarui: Dec 13, 2009 ⏰

Tambahkan cerita ini ke Perpustakaan untuk mendapatkan notifikasi saat ada bab baru!

Visual Studio C++ 2008 Part 2Tempat cerita menjadi hidup. Temukan sekarang