Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>How do I store this so that the data is stored to the heap?</p> </blockquote> <p>If you need <em>reference semantics</em>, i.e. if you need the values in the container to be aliases for values which are also referred to from other parts of the code, and modifications made in one part of the code should be visible for other parts that hold an alias to the modified <code>Data</code> object, I would say this is the right container definition:</p> <pre><code>std::vector&lt;std::vector&lt;std::shared_ptr&lt;Data&gt;&gt;&gt; </code></pre> <p>For what concerns your question about where the storage comes from, <code>std::vector</code> <em>always</em> allocates its elements dynamically in a continuous region of storage, no matter whether those are <code>shared_ptr</code>s, <code>vector</code>s, or <code>Data</code>s. </p> <p>However, I would recommend you to think if you really need reference semantics, or if it is not enough to store objects of type <code>Data</code> by value inside the containers:</p> <pre><code>std::vector&lt;std::vector&lt;Data&gt;&gt; </code></pre> <p>This would simplify your code and you would also get rid of the <code>shared_ptr</code> memory and run-time overhead. </p> <p>Whether or not you need reference semantics is something that only you, as the designer of your application, can tell. The information you provided is not enough for me to tell it without uncertainty, but hopefully this answer gave you a hint on the kind questions you should ask yourself, and what would be the answer in each case.</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