xHarbour Reference Documentation > Function Reference |
Retrieves the pointer to a method.
HB_ObjMsgPtr( <oObject>, <cMethod> ) --> nMethodPointer
The function returns a pointer to the method <cMethod> of the object <oObject>, or NIL, if the object does not have the specified method.
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.
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 |
// 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
http://www.xHarbour.com