现在切分地图所使用的投影 .

Delegate

Introduction


As a variable can be declared as a pointer so can a function. 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 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 reference 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 as 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, or can reference only, a member of a class
  Delegate Declaration
 
 

To declare a delegate, you use the __delegate keyword. The basic formula used to create a delegate is:

Access Level __delegate Function-Signature;
The Function-Signature factor is created as you would declare a normal C++ function. This means that the Function-Signature must have

A return type: The return type can be void, one of the primitive data types (int, long, double, short, etc), a .NET Framework type (Double, Decimal, etc), a .NET Framework class, or a class you created
A name: Like every variable, function, or class, a delegate must have a name
Parentheses: Like every function, a delegate must have a name
Optional arguments: If the method(s) that this delegate will reference has/have argument, this delegate must also have argument(s) of exactly the same kind)
After defining the Function-Signature, you must end the delegate declaration with a semi-colon. Here is an example:

__delegate double Addition();
A declaration of a delegate can also use an access level as public or private. If you plan to use the delegate only locally, you can omit the access level or declare it as private, which is the default. If you plan to use the delegate outside of the project in which it is created, you should declare it as public.

After declaring a delegate, remember that it only provides a skeleton for a method, not an actual method. In order to use it, you must define a method that would carry an assignment the method is supposed to perform. That method must have the same return type and the same (number of) argument(s), if any. Here is an example:

private __delegate double Addition();

__gc class MathOperations
{
public:
 double Plus()
 {
  double a = 248.66, b = 50.28;

  return a + b;
 }
};
After implementing the method, you can associate it to the name of the delegate. To do that, where you want to use the method, first declare a pointer of the type of the delegate using the new operator and calling its default constructor.

Techniques of Using a Delegate

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值