- overload () 並回傳自己的referernce
- overload cast (), 並回傳map
template<class keyType, class valueType>
class MapInitializer {
public:
MapInitializer(const keyType& key, const valueType& val) {
m_map[key] = val;
}
MapInitializer<keyType, valueType>& operator()(const keyType& key, const valueType& val) {
m_map[key] = val;
return *this;
}
operator std::map<keyType, valueType>() {
return m_map;
}
private:
std::map<keyType, valueType> m_map;
};
int _tmain(int argc, _TCHAR* argv[])
{
std::map<int, int> myMap = MapInitializer<int, int>(1, 2)(3, 4)(5, 6);
return 0;
}
沒有留言:
張貼留言