Personally, I think the source pointer (or link) is here. Smart pointers are associated with controlling the lifetime of the object that it points to, in which case MyMapper does not control the lifetime of this object, MyClass is. You also should not have a smart pointer pointing to an object that was not dynamically allocated (in this case, the hash map is not).
Personally, I would use something like the following:
class MyMapper { public: MyMapper(HashMap<string, Id> &map) : _map(map) { } private: HashMap<string, Id> &_map };
Please note that this will prevent the use of the MyMapper assignment operator and can only work if it is acceptable to pass the HashMap in the constructor; if this is a problem, I would make the element a pointer (although I would still pass the argument as a reference and make _map(&map) in the list of initializers).
If it is possible that MyMapper or any other class using a hash map will go into MyClass , then you will need to start thinking about smart pointers. In this case, I would recommend std::shared_ptr , but you have to use it everywhere: _hugeIdMap should be shared_ptr for a dynamically distributed value, and not for a regular field without a pointer.
Update:
Since you said that using a link is unacceptable due to project coding standards, I would suggest just sticking to the raw pointer for the reasons mentioned above.
source share