xHarbour Reference Documentation > Function Reference |
Executes a function, procedure or method from its pointer.
HB_Exec( pFunction_or_Method_Pointer [, <NIL_or_SelfObject> [, ...] ] ) or HB_Exec( pSomeFunctionPinter [, NIL, Arg1 [, ArgN] ] ) or HB_Exec( pSomeFunctionPinter [, Self, Arg1 [, ArgN] ] )
HB_Exec() returns the result of the executed function, method or procedure.
Function HB_Exec() accepts as first parameter a pointer to a function, procedure or method. If a function/procedure pointer is used, the second parameter must be NIL. In case of a method pointer, the second parameter must be an object. All other optional parameters are passed on to the executed function, procedure or method.
A pointer is a special data type in xHarbour and is of Valtype()=="P". It represents the memory address of a function, procedure or method. If a memory address is known, the corresponding routine can be executed without the need of looking up the symbolic name. This can lead to performance advantages.
See also: | @(), Eval(), HB_ExecFromArray(), HB_FuncPtr(), HB_ObjMsgPtr() |
Category: | Indirect execution , Pointer functions , xHarbour extensions |
Source: | vm\eval.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example uses a function pointer of a built-in function and // a method pointer to a user-defined class for HB_Exec(). #include "hbclass.ch" PROCEDURE Main LOCAL oObject LOCAL pPointer := ( @QOut() ) // displays "Hello World" HB_Exec( pPointer, NIL, "Hello", "World" ) oObject := Test():new( "xHarbour" ) pPointer := HB_ObjMsgPtr( oObject, "display" ) // displays five times "xHarbour" HB_Exec( pPointer, oObject, 5 ) RETURN CLASS Test PROTECTED: DATA name EXPORTED: METHOD init CONSTRUCTOR METHOD display ENDCLASS METHOD init( cName ) CLASS Test ::name := cName RETURN self METHOD display( nTimes ) CLASS Test LOCAL i IF nTimes == NIL nTimes := 1 ENDIF FOR i:=1 TO nTimes QOut( ::name ) NEXT RETURN self
http://www.xHarbour.com