🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.lenguajes.c++
Article #40

Re: Como pasar un puntero a función miembro como parametro de otra función

#40
From: Pascal J. Bourguignon
Date: Wed, 08 Oct 2008 12:31
88 lines
4003 bytes
jrbcast@gmail.com writes:

> On 7 oct, 14:44, p...@informatimago.com (Pascal J. Bourguignon) wrote:
>> jrbc...@gmail.com writes:
>> > Hola,
>>
>> > Tengo el siguiente problema:
>>
>> > class A
>> > {
>> > � � double ejemplo(double *p)
>> > � � {
>> > � � � �return do_job(p+1);
>> > � � }
>> > }
>>
>> > Ahora resulta que otra funci�n de externa a esta clase toma como
>> > par�metro un
>> > puntero a una funci�n, m�s concretamente:
>>
>> > void foo( double(*f)(double* x) )
>>
>> > Me gustar�a pasar el m�todo "ejemplo" como argumento a la funci�n foo.
>> > �C�mo lo puedo hacer?.
>>
>> > Gracias de antemano.
>>
>> Habr�a:
>>
>> � � std::mem_fun_ptr(&A::ejemplo)
>>
>> pero eso no te daria una funcion double(double*), porque una funcion
>> miembro toma tambien el parametro 'this'.
>>
>> Tendras que usar boost::bind:
>>
>> � � foo( boost::bind(A::ejemplo,this,_1) )
>> � � foo( boost::bind(A::ejemplo,alguno_objeto_de_clase_A,_1) )
>
>
> Gracias, pero me temo que no puedo usar boost :-(, mo me dejan a�adir
> librer�as
> a la que ya tenemos. �Sabes si esto se puede hacer de otra forma,
> digamos std:: o algo?.

Si, se puede hacer con std, pero es m�s trabajo.

Vease std::unary_function y std::binary_function 
http://www.cplusplus.com/reference/misc/functional/unary_function.html
http://www.cplusplus.com/reference/misc/functional/binary_function.html

Se puede escribir algo como:

struct double_de_double_star_funcion : public std::unary_function<double*,double> {
    A* that;
    double (A::* method)(double*);
    double operator()(double* arg){ return(that->method(arg)); }
}

double f(A* unObjeto){
    double_de_double_star_funci�n fun;
    fun.that=unObjeto;
    fun.method=(&A::ejemplo);
    foo(fun); 
}

//  8 l�neas en lugar de foo(boost::bind(A::ejemplo,unObjeto,_1))

pero hace falta que foo tome la funci�n struct en lugar de una pura funci�n C :

void foo(double_de_double_star_funcion fun){
     double arg=1.2d3;
     double res=fun(&arg);
     // ...
}


Si foo debe trabajar con varias clases de funci�n, se puede transformar en template:

template <class Fun> void foo(Fun fun){
     double arg=1.2d3;
     double res=fun(&arg);
     // ...
}


-- 
__Pascal Bourguignon__
Technical Details
Message-ID: <7ck5cj9sl2.fsf@pbourguignon.anevia.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!cleanfeed2-b.proxad.net!nnrp11-1.free.fr!not-for-mail
References: <4a22be7f-4b21-495b-91da-271ee842ded1@k7g2000hsd.googlegroups.com> <7cmyhg4ls4.fsf@pbourguignon.informatimago.com> <714716d7-0930-4d7e-b96c-c467d88f7d55@v30g2000hsa.googlegroups.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f53ec,7827fb6d5a442b8e
X-Google-Attributes: gidf53ec,domainid0,public,usenet
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!cleanfeed2-b.proxad.net!nnrp11-1.free.fr!not-for-mail
Newsgroups: es.comp.lenguajes.c++
Subject: Re: Como pasar un puntero a función miembro como
 parametro de otra función
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA
      oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9
      033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac
      l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR
      mV+hO/VvFAAAAABJRU5ErkJggg==
X-Accept-Language:         fr, es, en
X-Disabled: X-No-Archive: no
References: <4a22be7f-4b21-495b-91da-271ee842ded1@k7g2000hsd.googlegroups.com> <7cmyhg4ls4.fsf@pbourguignon.informatimago.com> <714716d7-0930-4d7e-b96c-c467d88f7d55@v30g2000hsa.googlegroups.com>
From: pjb@informatimago.com (Pascal J. Bourguignon)
Date: Wed, 08 Oct 2008 14:31:05 +0200
Message-ID: <7ck5cj9sl2.fsf@pbourguignon.anevia.com>
Organization: Anevia SAS
User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.2 (gnu/linux)
Cancel-Lock: sha1:NzhlZTY1MWY4Yjk0MGNkMzQwYWU2NmZhMmY1YTgyZWQ3NzVhZmE0ZA==
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Lines: 88
NNTP-Posting-Date: 08 Oct 2008 14:31:05 MEST
NNTP-Posting-Host: 88.170.236.224
X-Trace: 1223469065 news-2.free.fr 21098 88.170.236.224:53993
X-Complaints-To: abuse@proxad.net