功能指针v / s Delgate和事件 [英] Function Pointer v/s Delgate and Event

查看:83
本文介绍了功能指针v / s Delgate和事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

缺少指针功能是什么,以便我们转移到c#

What were the lacks of Pointer To Function so that we moved to Delegates and Events in c#

推荐答案

中的委托和事件。功能指针在C的美好时光中表现良好。但是在面向对象编程语言中,您希望能够存储指向成员函数的指针。 C ++中内置了对它的支持。这些成员函数指针的问题在于你总是需要做两件事来调用函数:



- 成员函数指针

- 一个可以应用它们的类对象。



将这两个东西放在一起或将它们作为两个独立的参数传递显然有点不合适。那么为什么不捆绑在一起? ......代表出生了。基本上,委托只是对象指针的复合对象加上成员函数指针。代表可以轻松地以类对象和成员函数的形式为软件的另一个组件提供回调点。该主体在窗口框架中被大量使用。在这里,您希望能够将某些窗口消息定向到窗口对象的某些成员函数。



简而言之,函数指针在简单程序世界中的作用已经被面向对象世界的代表们采用了。
Function pointers served well in the good old days of C. But in object oriented programming languages you want to be able to store pointers to member functions. Support for that was built into C++. The problem with such member function pointers is that you always need two things to make a function call:

- the member function pointer
- a class object on which to apply them.

It is obviously kind of inellegant to keep these two things together or pass them as two separate parameters. So why not bundle them together? ... And the delegate was born. Basically, a delegate is just a compound object of an object pointer plus a member function pointer. Delegates make it easy to give another component of your software a callback point in the form of a class object and member function. This principal is used heavily in windowing frameworks. Here you wish to be able to direct certain window messages to certain member functions of the window object.

In a nutshell, the role of function pointers in the simple procedural world, has been taken by delegates in the object oriented world.


让我们在接下来的几分钟内原谅我们没有使用官方正确的措辞:委托是面向对象语言的函数指针。代表有两个边:一方由呼叫者使用,另一方由接收呼叫的东西使用/实现。在调用端,委托看起来像一个普通的函数/函数指针,它与对象无关(!!!)。委托的另一方(实际委托实施)将来电转发给某事。事实上,这个东西可以是正常的函数(或面向对象语言的函数:静态方法),而不仅仅是某些语言中的实例方法!委托者不仅指向正常函数而且指向对象(实例)的方法的能力只是函数指针的扩展。无论如何,一个方法不仅仅是一个普通函数:你可以把它看作一个接收额外隐藏参数的正常函数(这个,自己,等等......)。在调用数组METHOD(如array_pointer-> GetSize())时调用数组函数(如Array_GetSize(array_pointer)),我们只是在方法的情况下以不同的方式传递隐藏的this指针(array_pointer),而不是在参数列表中但是array_pointer->字首。当委托指向实例方法时,会发生以下情况:委托本身将函数指针存储到方法中,该方法接收与委托的调用方完全相同的参数,但此外该方法需要额外的this指针。出于这个原因,当委托指向方法而不是旧学校函数时,委托必须将此额外隐藏此参数的值存储在方法/函数指针旁边,因为委托的调用者不会提供它并且委托需要当使用方法指针中继来电时。额外隐藏的this指针由委托的创建者提供,该委托知道隐藏this指针的值(将使用其中一种方法接收事件的实例)。委托的调用者对隐藏此参数的值(关于接收事件的对象/实例)不感兴趣。



编辑:事件就像一组函数指针(抱歉委托)。它提供与代理的呼叫方相同的类似功能的接口。当您触发事件时,它只会将调用转发给集合中的所有代理。它就像一群听众。与旧语言有什么不同:新语言为您提供语法,使您更容易声明和使用函数指针并使用函数指针数组。
Lets forgive ourselves for the next few minutes for not using the officially correct phrasing: A delegate IS the function pointer of the object oriented languages. The delegate has two "sides": One side is used by the caller, and the other side is used/implemented by something that receives the call. On the "call" side the delegate looks like a normal function/function pointer and it has nothing to do with objects (!!!). The other side (the actual delegate implementation) of the delegate relays the incoming calls to "something". This "something" can, in fact, be a normal function (or the function of object oriented languages: a "static method") and not only an instance method in some languages!!! The ability of a delegate to point not only to a normal function but also to a method of an object (an instance) is just an extension to function pointers. Anyway, a method is not much more than a normal function: you can think of it as a normal function that receives an extra hidden parameter (this, self, whatever...). You call an array function like Array_GetSize(array_pointer) while you are calling an array METHOD like array_pointer->GetSize(), we just passed the hidden this pointer (array_pointer) differently in case of a method, not in the parameter list but with the "array_pointer->" prefix. When a delegate points to an instance method then the following happens: The delegate itself stores a "function pointer" to the method that receives exactly the same parameters as the call side of the delegate but in addition the method expects an extra this pointer. For this reason the delegate must store the value of this extra hidden this parameter next to the method/function pointer when the delegate points to a method instead of an old-school function because the caller of the delegate will not provide it and the delegate need it when the incoming call is relayed by using the method pointer. The extra hidden this pointer is provided by the creator of the delegate who knows the value of the hidden this pointer (the instance that will receive the event with one of its methods). The caller of the delegate is not interested about the value of the hidden this parameter (about the object/instance that receives the event).

An event is like a collection of function pointers (sorry delegates). It provides the same "function-like interface" as the "call-side" of the delegate. When you fire the event it just relays the call to all delegates that are in the collection. Its like an array of listeners. What is different from older languages: new languages provide you with syntax that make it easier for you to declare and use "function pointers" and use "arrays of function pointers".


它不是缺乏就这样,它只是一种不同的思考方式。

C是一种过程语言,C#是事件驱动的。事件处理需要一种方法来指向一个函数,但由于指针在C#中被认为是危险的(出于好的理由),因此创建了委托以接受该角色。它实质上是一个函数指针,但内置了类实例感知:如果执行Delegate函数,则可以访问它所调用的类实例的属性,字段和方法。
It's not a "lack" as such, it's just a different way of thinking about things.
C is a procedural language, C# is event driven. The Event handling requires a way to "point" at a function, but since pointers are considered dangerous in C# (for good reasons) the Delegate was created to take that role. It is in essence a function pointer, but with class instance awareness built in: if you execute a Delegate function, you have access to the properties, fields and methods of the class instance on which it it called.


这篇关于功能指针v / s Delgate和事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆