mem_fun and bind1st problem

前端 未结 2 1353
小蘑菇
小蘑菇 2021-01-22 00:07

I\'ve following class:

class A {
public:
// ctr and etc ...
A*   clone(B* container);
};

Now, I\'ve a vector availableObjs

相关标签:
2条回答
  • 2021-01-22 00:12

    You need to use bind2nd instead of bind1st:

    transform(availableObjs.begin(), availableObjs.end(), back_inserter(clonedObjs),
        bind2nd(mem_fun(&A::clone), container)); // container is of type B*
    

    The functor created by mem_fun(&A::clone) expects an A* as its first parameter. This is the normally implicitly specified instance on which the method is called. The first "real" parameter of A::clone is the second parameter of mem_fun(&A::clone) and therefore needs to be bound with bind2nd.

    0 讨论(0)
  • 2021-01-22 00:37

    If you use Boost.Bind it might look like this:

    std::transform(
                   availableObjs.begin(), availableObjs.end(), 
                   back_inserter(clonedObjs),
                   boost::bind<A*>(boost::mem_fn(&A::clone), _1, container) ); 
    
    0 讨论(0)
提交回复
热议问题