|
Boost Users : |
From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2006-02-16 16:44:36
Rearranged reply:
Meryl Silverburgh wrote:
>
> On 2/16/06, Peter Dimov <pdimov_at_[hidden]> wrote:
>> Meryl Silverburgh wrote:
>>> Thanks how can I call a function with 2 more parameters, like this:
>>> How can I pass 'param1', 'param2' to the fuction doStuff?
>>>
>>> void A::aFunction( float param1, float param2) {
>>>
>>> vector<int> aVector;
>>> aVector.push_back(1);
>>> aVector.push_back(2);
>>> aVector.push_back(3);
>>> aVector.push_back(4);
>>>
>>> // want to loop thru the list and call A::print(int i, float
>>> param1, float param2)?
>>
>> for_each( aVector.begin(), aVector.end(),
>> bind( &A::doStuff, this, _1, param1, param2 )
>> );
>>
>>> }
>>>
>>>
>>> void A::doStuff(int i, float param1, float param2) {
>>>
>>> }
> Thanks. One more question, why the function 'doStuff' has to be
> 'public' otherwise, it won't compile? In my example, aFunction is a
> member function of 'A', so I can put 'doStuff' as private, right? but
> that won't compile.
>
>> for_each( aVector.begin(), aVector.end(),
>> bind( &A::doStuff, this, _1, param1, param2 )
>> );
>
> Can someone please tell me why?
Not sure what your exactly asking here.
for_each is expecting to call a function f( value_type& ), where in this
case value type is an int. for_each call this function f for each
derefrenced iterator in the sequence v.begin() to v.end(). The bind call
above returns such a function, mapping the int in f, to the placeholder in
the call to A::doStuff.
The assumption is made that A is a class rather than a name space, since you
didn't post a complete example. So that makes A::doStuff a member function.
Member functions take a this pointer as their hidden 1st argument.
A::aFunction is assumed to be a member function as well so the this pointer
is available.
bind( &A::doStuff // an A member function
, this // this instance of A's this pointer
, _1 // the derefenced vector::iterator (an int )
, param1 // the 1st float arg value
, param2 // the 2nd float arg value
)
Jeff Flinn
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net