将方法指针转换为整数,然后调用它 [英] Convert method pointer to integer, then call it

查看:82
本文介绍了将方法指针转换为整数,然后调用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以进行以下操作,如果可以,怎么办?请使用代码示例。

I'm wondering if this the following is possible or not, if yes, how? Code example please.


  • 如何将指向对象方法的指针存储为整数

  • 如何将该整数值转换回方法指针并调用它

  • How to store a pointer to a method of a object as an integer value?
  • How to convert that integer value back to a 'method pointer' and call it?

我想做的是在TComponent派生对象的整数Tag值中存储一个方法指针,然后稍后再调用存储的方法。您可以假定所有满足的方法都具有相同的定义。

What I want to do is store a 'method pointer' in the integer Tag value of a TComponent-derived object, and sometime later call the stored method. You can assume all met methods have the same definition.

谢谢!

推荐答案

您可以解决,但是它不是很好的设计...

You can do workaround, but it si not nice design...

var
  Method: ^TNotifyEvent;
begin
//Create New method 
  GetMem(Method, SizeOf(TNotifyEvent));
//Init target Tag
  Tag := Integer(Method);

//Store some method
  Method^ := Button1Click;

//call stored method
  Method := (Pointer(Tag));
  Method^(self);

//And don't forget to call in to object destructor...
  if Tag <> 0 then
    FreeMem(pointer(Tag));

这篇关于将方法指针转换为整数,然后调用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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