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

15.3.7Delegates Compositions

One of the characteristics that set delegates apart from C/C++ function pointers is that one delegate can be added to another using the overloaded addition assignment operator. This is referred to as composition.

15.3.8A Delegate With One of More Arguments

If you want to associate a method that takes arguments to a delegate, when declaring the delegate, provide the necessary argument(s) in its parentheses. Here is an example of a delegate that takes two arguments (and returns a value):

delegate double Addition(double x, double y);

When defining the associated method, besides returning the same type of value if not void, make sure that the method takes the same number of arguments. Here is an example:

private delegate double Addition(double x, double y);

private delegate double Multiplication(double x, double y);

public ref class CMathOperations

{

public:

double Plus(double x, double y)

{

return x + y;

}

};

Once again, to associate the method, declare a variable of the type of delegate and pass a reference to the method as the second argument to the constructor of the delegate. Here is an example:

private delegate double Addition(double x, double y);

private delegate double Multiplication(double x, double y);

public ref class CMathOperations

{

public:

CMathOperations()

{

add = gcnew Addition(&Plus);

multi = gcnew Multiplication(&Times);

}

static double Plus(double x, double y)

{

return x + y;

}

static double Times(double x, double y)

{

return x * y;

}

private:

Addition ^add;

Multiplication ^ multi;

};

Notice that only the name of the method is passed to the delegate. To actually use the value that the delegate produces, when calling its Invoke() method, you must pass the exact number of arguments with their correct values:

using namespace System;

private delegate double Addition(double x, double y);

private delegate double Multiplication(double x, double y);

public ref class CMathOperations

{

public:

CMathOperations()

{

add = gcnew Addition(&Plus);

multi = gcnew Multiplication(&Times);

}

static double Plus(double x, double y)

{

return x + y;

}

static double Times(double x, double y)

{

return x * y;

}

Void ShowResult()

{

Console::WriteLine(L"Addition = {0}",

add->Invoke(24.56, 98.76));

Console::WriteLine(L"Multiplication = {0}",

multi->Invoke(24.56, 98.76));

}

private:

Addition ^add;

Multiplication ^ multi;

};

Int main()

{

CMathOperations ^ oper = gcnew CMathOperations;

oper->ShowResult();

return 0;

}

This would produce:

Addition = 123.32

Multiplication = 2425.5456

15.3.9Practical Learning: Using an Argumentative Delegate

  1. To add an argument to a method, change the Flower.h header file as follows:  

#pragma once

using namespace System;

public ref class CFlower

{

private:

String ^ _tp;

String ^ _clr;

String ^ _arg;

double _price;

public:

. . .

public:

CFlower(void);

CFlower(String ^ type, String ^ color,

String ^ argn, double price);

~CFlower();