The magic of method pointers

The magic of method pointers
Date added/modified: 11 Nov 1999

You may not realize it but this is a concept at the core of Delphi technology and makes it truly amazing. You use it all the time without knowing about it. It might be interesting to see what this is all about.

I'll try to explain. Suppose you define a symbol TMyEvent with the following syntax:

 

type
  TMyEvent = 'a function or procedure prototype' of object;
Where, you substitute a desired function prototype in the above statement.

Now, if you declare a variable of the type TMyEvent, it's actually a method pointer. At runtime, it can point to any method of any object as long as its function or procedure prototype exactly matches what you defined above. What does this mean? At run time, some work of an object can be done by a method of another object. This is great! Here is how Delphi uses method pointers all the time to ease your work.

Look at the definition of TNotifyEvent in Delphi's help. It's declared as a method pointer:

type TNotifyEvent = procedure(Sender: TObject) of object;

Now see how the property OnClick is defined for the class TControl:

property OnClick: TNotifyEvent;

Internally, FOnClick is a variable of type TNotifyEvent in TControl. So, it's actually a method pointer. When you double-click on the OnClick event of a TButton in the Object Inspector, here is what Delphi does for you:

 

  1. Delphi creates a procedure of the TNotifyEvent type as a method of the form. For example:

     

    procedure MyForm.Button1Click(Sender: TObject);
    begin
    end;
  2. Then, it assigns this method to the OnClick property of that button which is internally assigned to the FOnClick method pointer.

     

  3. Now, if a click event occurs for the button, its TControl logic simply checks to see if FOnClick has been assigned a value (a method). If it does, it passes control to it.

     

If you think about it, the click event of the button is actually passed on to a method of the form. You can even change it at run time by assigning the property to any other method of any object as long as the prototype is same.

Once you understand this, you can do some very neat things which the environment doesn't provide for you. For example, visually, there is no way to combine one or more components to form a composite component. In other words, a Form can't become a visual component. But, if you do in code what Delphi does in Object Inspector, you can do it. You can put all those components inside a Panel, then dynamically assign events to the panel's methods. Yes, it's possible but you need to do everything in code.

I have tried this approach to create a RichEdit control which carries its toolbar and ruler along with it.

 

 

Here is an example implementation of a function to compute the integral of any unary function using C language and pointers: ```c #include <stdio.h> double integral(double (*f)(double), double a, double b, int n) { double h = (b - a) / n; // width of each subinterval double sum = 0.0; // initialize the sum int i; for (i = 0; i < n; i++) { double x1 = a + i * h; double x2 = a + (i + 1) * h; double y1 = (*f)(x1); double y2 = (*f)(x2); double area = (y1 + y2) * h / 2.0; sum += area; } return sum; } // example usage double square(double x) { return x * x; } int main() { double a = 0.0; double b = 1.0; int n = 1000; double result = integral(&square, a, b, n); printf("Integral of x^2 from %g to %g with %d subintervals: %g\n", a, b, n, result); return 0; } ``` In this example, the `integral` function takes four arguments: a pointer to the unary function to integrate (`f`), the lower and upper bounds of integration (`a` and `b`, respectively), and the number of subintervals to use in the approximation (`n`). The function then computes the width of each subinterval (`h`), initializes the sum to 0, and iterates over the subintervals, computing the area under the curve for each and adding it to the sum. The final result is the sum of all the subinterval areas, which is returned by the function. To demonstrate the usage of this function, the `square` function is defined as an example of a unary function to integrate, and is passed as a pointer to the `integral` function along with the bounds of integration and number of subintervals. The result is printed to the console.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值