mem_fun和bind1st问题 [英] mem_fun and bind1st problem
问题描述
我有以下类:
class A {
public:
// ctr and etc ...
A* clone(B* container);
};
现在,我有一个向量< A *> availableObjs
已填充。我想在其中的每一个上调用 clone
,因此将克隆的对象插入一个新的容器 clonedObjs
类型 vector< A *>
。我试图跟着 - 但它不编译:
Now, I've a vector<A*> availableObjs
populated already. I want to call clone
on each of those, so and insert cloned objects into a new container clonedObjs
of type vector<A*>
. I'm trying following - but it doesn't compile:
transform(availableObjs.begin(), availableObjs.end(), back_inserter(clonedObjs),
bind1st(mem_fun(&A::clone), container)); // container is of type B*
有一个简单的方法吗?
Is there a easy way out? I've a lot classed like A - so making each of those a functor is too much task.
推荐答案
您需要使用 bind2nd
而不是 bind1st
:
transform(availableObjs.begin(), availableObjs.end(), back_inserter(clonedObjs),
bind2nd(mem_fun(&A::clone), container)); // container is of type B*
mem_fun ; A :: clone)
期望一个 A *
作为它的第一个参数。这是在其上调用该方法的通常隐式指定的实例。 A :: clone
的第一个真实参数是 mem_fun(& A :: clone)
,因此需要绑定 bind2nd
。
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
.
这篇关于mem_fun和bind1st问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!