I am am using std::unordered_map and I would like to use it to store pointers to an object that I dynamically create with new.
I can create the objects fine and the insert method succeeds; however when I attempt to resolve a specific object from the map using the index it appears that all of the items in the list are mapped to the last item I point in the map.
typedef std::unordered_map<__int64, Session*> HashDict; HashDict dict; void Sessions::Insert() { Session* newSession = new Session; newSession->setId(sessionCounter++); dict.insert(HashDict::value_type(newSession->Id(), newSession)); } void Sessions::Find(__int64 id) { HashDict::iterator it = dict.find(id); if(it != dict.end()) { Session* s = it->second; std::cout << "Search for: " << id << std::setw(3) << " found @ location: " << it->second << std::setw(3) << " with value: " << s->Id() << std::endl; } } 001D4F50 0 001D5038 1 001D50D0 2 001D5378 3 001D5410 4 001D54A8 5 001D5540 6 001D55D8 7 001D5670 8 001D4E50 9 Search for: 5 found @ location: 001D54A8 with value: 9 Notice that 5 points to the location at 5 (from the insert), however it says that the value is 9.
What am I doing wrong?
Session?Sessionobjects and note that setting the value on the ID of the first affects the ID of the second. Nothing to do withunordered_map.