2
我想將functors
存儲在stl map
中,然後逐個調用它,但現在確定如何調用它。這是我到目前爲止所嘗試的。在一個stl地圖中存儲函數並調用它們
#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
什麼不工作? –
有沒有一個原因,你說博士與邪惡引語'函子? –
運算符優先級需要'(* itr-> second)()'。 –