Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename Key, typename Value>
- class Proxy
- {
- std::map<Key, Value>& my_map_;
- Key key_;
- public:
- Proxy(std::map<Key, Value>& my_map, const Key& key) : my_map_(my_map), key_(key)
- {
- }
- operator Value()
- {
- std::cout << "Value()\n";
- if (my_map_.find(key_) == std::end(my_map_))
- return 0;
- return my_map_[key_];
- }
- const Value& operator=(const Value& value)
- {
- std::cout << "operator=\n";
- return my_map_[key_] = value;
- }
- };
- template <typename Key, typename Value>
- class A
- {
- std::map<Key, Value> my_map_;
- public:
- Proxy<Key, Value> operator[](const Key& key)
- {
- return Proxy<Key, Value>(my_map_, key);
- }
- const Value& operator[](const Key& key) const
- {
- std::cout << "const operator[]\n";
- auto found = my_map_.find(key);
- if (found == std::end(my_map_))
- return 0;
- return (found->second);
- }
- void Print() const
- {
- std::cout << "Map content:\n";
- for (auto const& it : my_map_)
- {
- std::cout << it.first << " " << it.second << "\n";
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement