我试图存储functors
在 astl map
然后一个一个地调用它,但现在确定如何调用它。这是我到目前为止所尝试的。
#include <iostream>
#include <map>
#include <string>
class BaseFunctor {
public:
BaseFunctor() {
}
~BaseFunctor() {
}
};
template <typename T>
class MyFunctor : public BaseFunctor {
public:
T operator()(T x) {
return x * 2;
}
};
int main ( int argc, char**argv ) {
std::map<std::string, BaseFunctor*> m_functorMap;
m_functorMap.insert(std::make_pair("int", new MyFunctor<int>()));
m_functorMap.insert(std::make_pair("double", new MyFunctor<double>()));
m_functorMap.insert(std::make_pair("float", new MyFunctor<float>()));
m_functorMap.insert(std::make_pair("long", new MyFunctor<long>()));
for ( std::map<std::string, BaseFunctor*>::iterator itr = m_functorMap.begin(); itr != m_functorMap.end(); ++itr ) {
std::cout << *(itr->second)() << std::endl;
}
return 0;
}
我不能使用boost