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

Int main()

{

CSquare Sqr;

double Side = 25.55;

Sqr.Perimeter = Perimetre;

Console::WriteLine("Square Characteristics");

Console::WriteLine("Side: {0}", Side);

Console::WriteLine("Perimeter: {0}", Sqr.Perimeter(Side));

return 0;

}

Using this same approach, you can declare different types of pointers to function as you see fit and using the function signature(s) of your choice. Here is an example:

using namespace System;

typedef double (*Multiply)(const double a);

typedef double (*Multiple)(const double x, const double y);

public value class CSquare

{

public:

Multiply Perimeter;

Multiple Area;

};

double Perimetre(const double x)

{

return x * 4;

}

double Surface(const double x, const double y)

{

return x * y;

}

Int main()

{

CSquare Sqr;

double Side = 25.55;

Sqr.Perimeter = Perimetre;

Sqr.Area = Surface;

Console::WriteLine("Square Characteristics");

Console::WriteLine("Side: {0}", Side);

Console::WriteLine("Perimeter: {0}", Sqr.Perimeter(Side));

Console::WriteLine("Area: {0}", Sqr.Area(Side, Side));

return 0;

}

This would produce:

Square Characteristics

Side: 25.55

Perimeter: 102.2

Area: 652.8025

In the class, we use the Multiply word as a type, the same way we would use a normal data type, to declare a variable. Although this looks like that, it follows different rules. While you can declare two variables using the same data type, you cannot declare two pointer to methods using the same type:

public value class CSquare

{

public:

// This works

double a, b;

// This produces an error

Multiply Perimeter, Area;

};

If you do this, you would receive an error when you compile the application.

15.3Delegates

15.3.1 Introduction

As a variable can be declared as a pointer so can a function. As we saw in the previous sections, a pointer to a function is a type of variable whose name can be used as a variable although it is not a traditional variable like the others. This concept has always been very helpful in Microsoft Windows programming because it allows the use of callback functions. Thanks to their effectiveness (and legacy code), the concept of callback functions was carried out in the .NET Framework but they were defined with the name of delegate.

A delegate is a special type of user-defined variable that references a method of a class. There are similarities and differences between a function pointer and a delegate:

  • Like a function pointer, a delegate is not defined like a normal function

  • Like a function pointer, a delegate has the appearance of a variable that is declared as a pointer

  • Like a function pointer, a delegate can take argument(s)

  • While a function pointer can be associated with a global function, that is, a function that is not a member of a class, a delegate can be associated with, or can reference, only a member of a class

15.3.2Practical Learning: Introducing Delegates

  1. Start Microsoft Visual C++ 2005

  2. On the main menu, click File -> New -> Project...

  3. On the left side, make sure that Visual C++ is selected. In the Templates list, click CLR Empty Project

  4. In the Name box, replace the string with FlowerShop1 and click OK

  5. On the main menu, click Project -> Add Class...

  6. In the Categories lists, expand Visual C++ and click C++. In the Templates list, make sure C++ Class is selected and click Add

  7. Set the Name of the class to CFlower and click Finish

  8. Complete 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:

property String ^ Type

{

String ^ get() { return _tp; }

void set(String ^ tp) { _tp = tp; }

}

property String ^ Color

{

String ^ get() { return _clr; }

void set(String ^ clr) { _clr = clr; }

}

property String ^ Arrangement

{

String ^ get() { return _arg; }

void set(String ^ arg) { _arg = arg; }

}

property double UnitPrice

{

double get() { return _price; }

void set(double price) { _price = price; }

}

public:

CFlower(void);

CFlower(String ^ type, String ^ color,

String ^ argn, double price);

~CFlower();