xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

HB_ObjMsgPtr()

Retrieves the pointer to a method.

Syntax

HB_ObjMsgPtr( <oObject>, <cMethod> ) --> nMethodPointer

Arguments

<oObject>
This is an object whose method pointer is retrieved.
<cMessage>
This is a character string holding the symbolic name of the method to retrieve the pointer for.

Return

The function returns a pointer to the method <cMethod> of the object <oObject>, or NIL, if the object does not have the specified method.

Description

Function HB_ObjMsgPtr() retrieves the pointer to a method of an object. A pointer is the memory address of a method. It can be used with HB_Exec() or HB_ExecFromArray() to execute a method from its pointer. This is similar to embedding a method call within a code block and passing the block to the Eval() function. A code block can contain multiple method calls. If only one method is to be executed indirectly, a method pointer along with HB_Exec() or HB_ExecFromArray() is faster than code block execution.

The advantage of a method pointer is that the dynamic lookup of the symbolic method name can be avoided once the pointer is obtained. This can improve the performance of time critical methods considerably when they must be called very often.

Info

See also:@(), CLASS, HB_Exec(), HB_ExecFromArray(), HB_FuncPtr(), METHOD (Declaration)
Category: Indirect execution , xHarbour extensions
Source:vm\classes.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example compares indirect method execution using
// a method pointer and a code block.

   #include "hbclass.ch"

   PROCEDURE Main
      LOCAL oObject  := Test():new( "xHarbour" )
      LOCAL pPointer := HB_ObjMsgPtr( oObject, "display" )
      LOCAL bBlock   := {|o| o:display() }

      HB_Exec( pPointer, oObject )

      Eval( bBlock, oObject )
   RETURN


   CLASS Test
     PROTECTED:
     DATA name

     EXPORTED:
     METHOD init CONSTRUCTOR
     METHOD display
   ENDCLASS

   METHOD init( cName ) CLASS Test
      ::name := cName
   RETURN self

   METHOD display CLASS Test
      QOut( ::name )
   RETURN self

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe