Pasar una funcion como parametro a otra funcion en un PRG...

Post Reply
xmanuel
Posts: 613
Joined: Sun Jun 15, 2008 7:47 pm
Location: Sevilla
Contact:

Pasar una funcion como parametro a otra funcion en un PRG...

Post by xmanuel »

Alguien sabe si se puede crear un puntero a una función y pasarlo como parámetro a otra donde será ejecutada...

En C es fácil pero en harbour alguien sabe cómo hacerlo?

Algo así:

Code: Select all

procedure main

    local pFunc, c
    
    pFunc := miFuncion()
    
    c := ejecutaFunc( pFunc, "Otra prueba" )
    
    Alert( c )

return

//------------------------------------------------------------------------------

static function ejecutaFunc( pf, cValue )

    local c := "Funcion ejecutaFunc() -> "
    
    c += pf( cValue )
    
return c


static function miFuncion( cParam )

    local cCadena := "Funcion miFuncion() -> "
    
    cCadena += cParam

return cCadena

//------------------------------------------------------------------------------

 
No sé si está entendido...
Ojo, sin usar codeblock ni macros...
______________________________________________________________________________
Sevilla - Andalucía
hmpaquito
Posts: 1200
Joined: Thu Oct 30, 2008 2:37 pm

Re: Pasar una funcion como parametro a otra funcion en un PRG...

Post by hmpaquito »

Hola Manuel,

"Another interesting innovation in the Harbour - pointers to functions. They can be created on the stage of building the application, using the expression @<funcName>(), or dynamically during execution with the help of the macro: &("@<funcName>()"). Valtype() returns S for such pointers."

Code: Select all

proc main()
   local funcSym
   funcSym := @str()
   ? funcSym:name, "=>", funcSym:exec( 123.456, 10, 5 )
   funcSym := &( "@upper()" )
   ? funcSym:name, "=>", funcSym:exec( "Harbour" )
return
 
Desde: http://www.kresin.ru/en/hrbfaq_3.html

Salu2
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: Pasar una funcion como parametro a otra funcion en un PRG...

Post by nageswaragunupudi »

hmpaquito wrote:Hola Manuel,

"Another interesting innovation in the Harbour - pointers to functions. They can be created on the stage of building the application, using the expression @<funcName>(), or dynamically during execution with the help of the macro: &("@<funcName>()"). Valtype() returns S for such pointers."

Code: Select all

proc main()
   local funcSym
   funcSym := @str()
   ? funcSym:name, "=>", funcSym:exec( 123.456, 10, 5 )
   funcSym := &( "@upper()" )
   ? funcSym:name, "=>", funcSym:exec( "Harbour" )
return
 
Desde: http://www.kresin.ru/en/hrbfaq_3.html

Salu2
Very good.
But this approach works only if the function is already linked to the application. Otherwise we get link errors.

There are times when we want a function to be executed, only if the function is already linked to the application and if not not to execute or do some other action.

Code: Select all

pMyFunc := HB_FUNCPTR( "MYFUNC" )
if Empty( pMyFunc )
   // not linked
else
  HB_ExecFromArray( pMyFunc, { param1, param2, ..., paramN } )
endif
 
Regards

G. N. Rao.
Hyderabad, India
Post Reply