Note that there are some explanatory texts on larger screens.

plurals
  1. POIterator vs. Reference vs. Pointer
    text
    copied!<p>I have a class that spawns an arbitrary number of worker object that compute their results into a <code>std::vector</code>. I'm going to remove some of the worker objects at certain points but I'd like to keep their results in a certain ordering only known to the class that <code>spawned</code> them. Thus I'm providing the vectors for the output in the class A.</p> <p>I have (IMO) three options: I could either have pointers to the vectors, references or iterators as members. While the iterator option has certain draw backs (The iterator could be incremented.) I'm unsure if pointers or references are clearer. I feel references are better because they can't be NULL and a cruncher would require the presence of a vector.</p> <p>What I'm most unsure about is the validity of the references. Will they be invalidated by some operations on the <code>std::list&lt; std::vector&lt;int&gt; &gt;</code>? Are those operations the same as invalidating the iterators of <code>std::list</code>? Is there another approach I don't see right now? Also the coupling to a container doesn't feel right: I force a specific container to the Cruncher class.</p> <p>Code provided for clarity:</p> <pre><code>#include &lt;list&gt; #include &lt;vector&gt; #include &lt;boost/ptr_container/ptr_list.hpp&gt; class Cruncher { std::vector&lt;int&gt;* numPointer; std::vector&lt;int&gt;&amp; numRef; std::list&lt; std::vector&lt;int&gt; &gt;::iterator numIterator; public: Cruncher(std::vector&lt;int&gt;*); Cruncher(std::vector&lt;int&gt;&amp;); Cruncher(std::list&lt; std::vector&lt;int&gt; &gt;::iterator); }; class A { std::list&lt; std::vector&lt;int&gt; &gt; container; boost::ptr_list&lt; std::vector&lt;int&gt; &gt; container2; std::vector&lt;Cruncher&gt; cruncherList; }; </code></pre>
 

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