Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
8.1 Mб
Скачать

Int main()

{

CMathOperations ^ oper = gcnew CMathOperations;

Addition ^ add = gcnew Addition(oper, &CMathOperations::Plus);

Console::WriteLine(L"Value = {0}", add->Invoke());

return 0;

}

This would produce:

Value = 298.94

We mentioned earlier that you could declare the associated method of a delegate like any normal method, in which case you must pass a pointer to the class as the first argument of the instance of the delegate. This is a requirement if you plan to instantiate the delegate outside of the class that defines its associated method. That's the case for our MathOperations class. If you plan to use a delegate in the same class that implements its associated method, you can declare the method static. Here is an example:

private delegate double Addition();

private delegate double Multiplication();

public ref class CMathOperations

{

public:

double Plus()

{

double a = 248.66, b = 50.28;

return a + b;

}

static double Times()

{

double a = 248.66, b = 50.28;

return a * b;

}

};

Once again, to use the delegate, you must first declare its pointer and allocate its memory using the gcnew operator and calling the constructor that takes two arguments. This time, the first argument must be null or 0. The second argument must still be a reference to the method that implements the desired behavior. Here is an example:

using namespace System;

private delegate double Addition();

private delegate double Multiplication();

public ref class CMathOperations

{

public:

CMathOperations()

{

multi = gcnew Multiplication(&Times);

}

double Plus()

{

double a = 248.66, b = 50.28;

return a + b;

}

static double Times()

{

double a = 248.66, b = 50.28;

return a * b;

}

Void ShowResult()

{

Console::WriteLine(L"Multiplication = {0}", multi->Invoke());

}

private:

Multiplication ^ multi;

};

Int main()

{

CMathOperations ^ oper = gcnew CMathOperations;

Addition ^ add = gcnew Addition(oper, &CMathOperations::Plus);

Console::WriteLine(L"Addition = {0}", add->Invoke());

oper->ShowResult();

return 0;

}

This would produce:

Addition = 298.94

Multiplication = 12502.6248

Because delegates are usually declared globally, that is, outside of a class, they can be associated with a method of any class, provided the method has the same return type (and the same (number of) argument(s)) as the delegate.

15.3.6Practical Learning: Creating a Delegate

  1. To implement and use a delegate, change the Exercise.cpp source file as follows:  

#include "Flower.h"

using namespace System;

private delegate void Display();

Int main()

{

CFlower ^ flower = gcnew CFlower;

flower->Type = L"Daisies";

flower->Color = L"Yellow";

flower->Arrangement = L"Vase";

flower->UnitPrice = 45.65;

Display ^ disp = gcnew Display(flower, &CFlower::Show);

disp->Invoke();

Console::WriteLine();

return 0;

}

  1. Execute the application to see the result:  

=======================

==-=-=Flower Shop=-=-==

-----------------------

Flower Type: Daisies

Flower Color: Yellow

Arrangement: Vase

Price: $45.65

=======================

  1. Close the DOS window