当前位置:首页 » 多媒体相关

有关函数指针?


function   test:   string;  
  begin  
      result   :=   function   pointer   test;  
  end;  
   
  procedure   tform1.button1click(sender:   tobject);  
  var  
      func:   function:   string;  
  begin  
      func   :=   test;  
      caption   :=   func;  
  end;  
  //上面的调用一切正常  
   
   
  //现在我将函数写入类中调用  
  tbase   =   class  
  public  
      function   test:   string;  
  end;  
   
  function   tbase.test:   string;  
  begin  
      result   :=   object   function   pointer   test;  
  end;  
   
  procedure   tform1.button2click(sender:   tobject);  
  var  
      base:   tbase;  
      func:   function:   string   of   object;  
  begin  
      base   :=   tbase.create;  
      try  
          //这样调用tmethod(func).code也是nil  
  //         tmethod(func).code   :=   base.methodaddress(test);    
          tmethod(func).code   :=   tbase.methodaddress(test);  
          if   assigned(tmethod(func).code)   then  
              caption   :=   func;  
      finally  
          base.free;  
      end;  
  end;  
  为什么tmethod(func).code还是nil,如果调用类中的函数指针应该怎样定义和调用?  
 

推荐阅读

  • 年度新锐榜:MOTO捧回两项桂冠 [详细内容]
  • IT [详细内容]
  • 499元大气魄 256MB影驰GF5200现身 [详细内容]
  • IT [详细内容]
  • IT [详细内容]
  • 买爱国者数码相机伴侣 送256MB记忆棒 [详细内容]
  • 359没有电的情况下可以使用其他的3*线冲冲电哦 [详细内容]
  • 网友回答:
    网友:wx1452

    一个方法是这样   tmethod(func).code   :=   @tbase.test;  
      另一个方法是  
      tbase   =   class  
      published  
          function   test:   string;     //声明为公布的  
      end;

    网友:dwgz

    把tbase   改成这样  
          tbase   =   class  
          published  
              function   test:   string;  
          end;  
       
       
       
      returns   the   address   of   a   published   method.  
      =========================================  
       
      class   function   methodaddress(const   name:   shortstring):   pointer;  
       
      description  
       
      methodaddress   is   used   internally   by   the   vcl   streaming   system.   when   reading   an   event   property   from   a   stream,   methodaddress   converts   a   method   name,   specified   by   name,   to   a   pointer   containing   the   method   address.   there   should   be   no   need   to   call   methodaddress   directly.  
       
      if   name   does   not   specify   a   published   method   for   the   object,   methodaddress   returns   nil.

    .

    讨论区

    Login