C++ 使用std::vector<;T*>;::使用std::mem_fun和std::bind1st向后推
我试图使用C++ 使用std::vector<;T*>;::使用std::mem_fun和std::bind1st向后推,c++,templates,stl,mem-fun,C++,Templates,Stl,Mem Fun,我试图使用std::vector::push_back和std::mem_fun和std::binder1st,但这似乎不可行,可以做到吗 我试着用下面的代码举例说明 #include <vector> #include <functional> #include <iostream> using namespace std; struct A { int _Foo; virtual int AFoo() { return
std::vector::push_back
和std::mem_fun
和std::binder1st
,但这似乎不可行,可以做到吗
我试着用下面的代码举例说明
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
struct A {
int _Foo;
virtual int AFoo() { return _Foo; };
};
struct B: public A {
int BFoo(int bar) { return _Foo+bar ; };
};
struct C: public A {
int CFoo() { return --_Foo; };
};
class MyContainer
{
static const int MyArraySize = 100;
A* MyArray[MyArraySize];
public:
MyContainer() {
int half = MyArraySize / 2;
for( int i=0; i< half; ++i )
MyArray[i] = new B;
for( int i=half; i < MyArraySize; ++i )
MyArray[i] = new C;
}
template<class T, class Fn1>
int Execute( Fn1 func )
{
int count = 0;
for( int i=0; i< MyArraySize; ++i ){
T* t = dynamic_cast<T*>(MyArray[i]);
if( t )
{
func(t);
++count;
}
}
return count;
}
template<class T, class Res, class Arg>
int Execute( mem_fun1_t<Res, T, Arg> func, Arg argument )
{
return Execute<T>( binder2nd< mem_fun1_t<Res,T,Arg> >( func, argument ) );
}
template<class T>
vector<T*> GetItems() // <-- This is the problem function
{
vector<T*> ret;
Execute<T>( bind1st( mem_fun(&vector<T*>::push_back), ret ) );
return ret;
}
};
int main( int argc, char* argv[] )
{
MyContainer cont;
cont.Execute<B>( mem_fun(&B::BFoo), 10 );
cont.Execute<C>( mem_fun(&C::CFoo) );
vector<B*> v = cont.GetItems<A>(); // <-- the problem function is called here.
cout << "v.size = " << v.size() << endl;
}
#包括
#包括
#包括
使用名称空间std;
结构A{
int_Foo;
虚拟int-AFoo(){return\u-Foo;};
};
结构B:公共A{
intbfoo(intbar){return\ufoo+bar;};
};
结构C:公共A{
int CFoo(){return--_Foo;};
};
类霉菌容器
{
静态常数int MyArraySize=100;
A*MyArray[MyArraySize];
公众:
MyContainer(){
int-half=MyArraySize/2;
对于(int i=0;i(func,argument));
}
模板
vector GetItems()//对容器中的整个项目集调用成员函数的最简单方法是对每个项目使用:
using namespace std;
using namespace std::tr1;
vector<T> cont;
// ...
for_each( cont.begin(), cont.end(),
bind( &T::foo, 42 ) );
// assume void T::foo(int); exists
但我不确定您在这里想要实现什么。您既有编译时多态性(又名模板)也有运行时多态性(又名virtual
成员函数)。设计似乎有点太复杂。事实上,以下定义就足够了:
int Execute()
{
int count = 0;
for( int i=0; i< MyArraySize; ++i ){
MyArray[ i ]->Foo(); // assume virtual int A::Foo(); exists
++count;
}
return count;
}
问题在于binder1st将运算符()定义为:
mem_fun1_t将运算符()定义为:
问题在于推回定义为:
void vector<T>::push_back(const T &x)
换句话说,引用指针的引用。C++中不存在引用的引用。我认为修复这个问题的唯一正确方法是使用Booo::
vector<T*> ret;
Execute<T>( boost::bind( mem_fun(&vector<T*>::push_back), &ret, _1) );
return ret;
vectorret;
执行(boost::bind(mem_-fun(&vector::push_-back),&ret,_-1));
返回ret;
还请注意,您有一个bug,需要传递bind&ret而不是ret(因为mem\u-fun需要一个指针,mem\u-fun\u-ref可以工作).我实际上使用的VS2005不支持std::tr1,我编辑了这个问题来更新我的目标。我知道我可以将数组复制到向量,我真的很想知道我到底做错了什么…VS2005对tr1的支持有限。虽然IIRCtr1::bind
不可用。但是,如果您试图调用成员functi在A
或其子类上,每个的应该不会有什么变化。请参阅我的更新答案。该错误是一个输入错误,创建了一个示例,但谢谢!Boost现在不是一个真正的选项。=/您可以随时滚动自己的bind1st函数和binder1st结构。只需复制它们的实现,但将const T&T更改为T。这样,您就不会以双重引用结束。
operator() (const typename Operation::second_argument_type& x) const
S operator() (T* p, A x) const
void vector<T>::push_back(const T &x)
void mem_fun1_t::operator()(vector<T *> *p, const T *&x)
void binder1st::operator()(const T *&&x)
vector<T*> ret;
Execute<T>( boost::bind( mem_fun(&vector<T*>::push_back), &ret, _1) );
return ret;