Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Personally I think a raw pointer (or reference) is okay here. Smart pointers are concerned with managing the lifetime of the object pointed to, and in this case <code>MyMapper</code> isn't managing the lifetime of that object, <code>MyClass</code> is. You also shouldn't have a smart pointer pointing to an object that was not dynamically allocated (which the hash map isn't in this case).</p> <p>Personally, I'd use something like the following:</p> <pre><code>class MyMapper { public: MyMapper(HashMap&lt;string, Id&gt; &amp;map) : _map(map) { } private: HashMap&lt;string, Id&gt; &amp;_map }; </code></pre> <p>Note that this will prevent <code>MyMapper</code> from having an assignment operator, and it can only work if it's acceptable to pass the HashMap in the constructor; if that is a problem, I'd make the member a pointer (though I'd still pass the argument as a reference, and do <code>_map(&amp;map)</code> in the initializer list).</p> <p>If it's possible for <code>MyMapper</code> or any other class using the hash map to outlive <code>MyClass</code>, then you'd have to start thinking about smart pointers. In that case, I would probably recommend <code>std::shared_ptr</code>, but you'd have to use it everywhere: <code>_hugeIdMap</code> would have to be a <code>shared_ptr</code> to a dynamically allocated value, not a regular non-pointer field.</p> <hr /> <p><strong>Update:</strong></p> <p>Since you said that using a reference is not acceptable due to the project's coding standards, I would suggest just sticking with a raw pointer for the reasons mentioned above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload