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

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

Postby xmanuel » Sat May 26, 2018 9:14 am

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  Expand view

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
xmanuel
 
Posts: 756
Joined: Sun Jun 15, 2008 7:47 pm
Location: Sevilla

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

Postby hmpaquito » Sat May 26, 2018 10:47 am

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  Expand view
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
hmpaquito
 
Posts: 1482
Joined: Thu Oct 30, 2008 2:37 pm

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

Postby nageswaragunupudi » Sun May 27, 2018 10:27 am

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  Expand view
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  Expand view

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

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10308
Joined: Sun Nov 19, 2006 5:22 am
Location: India


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 13 guests