Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Change </p> <pre><code>std::vector&lt;Class2&gt; c2v; </code></pre> <p>to</p> <pre><code>std::vector&lt;Class2 *&gt; c2v; </code></pre> <p>and </p> <pre><code>std::vector&lt;Class3&gt; c3v; </code></pre> <p>to</p> <pre><code>std::vector&lt;Class3*&gt; c3v; </code></pre> <p>The problem is that you are taking adresses of local objects inside vector. When vector need more space it reallocates his memory and therefore <code>Class2</code> and <code>Class3</code> objects having their adresse changed.</p> <pre><code>class Class1 { public: std::vector&lt;Class2 *&gt; c2v; Class1(); }; class Class2 { Class1 *instance; int id; public: std::vector&lt;Class3 *&gt; c3v; Class2 ( Class1 *instance, int id ); }; class Class3 { Class1 *instance; int id; public: Class3 ( Class1 *instance, int id ); }; Class1::Class1() { for ( int i = 0; i &lt; noi; ++i ) { c2v.push_back ( new Class2 ( this, i ) ); } } Class2::Class2 ( Class1 *instance, int id ) { this-&gt;instance = instance; this-&gt;id = id; for ( int k = 0; k &lt; nok; ++k ) { c3v.push_back ( new Class3 ( this-&gt;instance, k ) ); } } </code></pre> <p>And don't forget to cleanup in Class1' and Class2' destructors.</p> <p>EDIT: Why such strange behaviour occured:</p> <p>Step 1</p> <pre><code>Ctqd::Ctqd() { for ( int i = 0; i &lt; nodes; ++i ) { suppliers.push_back ( Supplier ( this, i ) ); // &lt;-- Supplier's constructor is invoked here // to construct automatic object of Supplier class } } </code></pre> <p>Step 2. We are inside of the automatic object's constructor:</p> <pre><code>Supplier::Supplier ( Ctqd *instance, int id ) { for ( int k = 0; k &lt; numProdotti; ++k ) { offers.push_back ( Offer ( this, k ) ); // consider that we are passing this to // Offer's constructor. this is a pointer to // automatic variable } } </code></pre> <p>Step 3. Back to step 1. But now we have automatic object Supplier created</p> <pre><code>Ctqd::Ctqd() { for ( int i = 0; i &lt; nodes; ++i ) { suppliers.push_back ( Supplier ( this, i ) ); // &lt;-- automatic object of class Supplier // is created. Now as long as push_back() taking // parameter by value yo are passing copy of // automatic object to push_back(); // as long as you didn't define copy constructor // for Supplier compiler adds default for you. } } </code></pre> <p>Step 4. Copy of automatic object is saved to <code>suppliers</code> vector. New object (copy of automatic object) has vector <code>offers</code> with exactly same values as our automatic object had (thx to vector's copy constructor). So each object has member <code>supplier</code> that points to... (geass what :)) correct! they all point to automaic object.</p> <pre><code>Ctqd::Ctqd() { for ( int i = 0; i &lt; nodes; ++i ) { suppliers.push_back ( Supplier ( this, i ) ); // &lt;-- magic here } } </code></pre> <p>Step 5. Automatic object has been destructed(remember how you passed point to this object to Offer's constructor?). Oops you say.</p> <pre><code>Ctqd::Ctqd() { for ( int i = 0; i &lt; nodes; ++i ) { suppliers.push_back ( Supplier ( this, i ) ); // &lt;-- automatic doesn't exist no more } } </code></pre> <p>So what will happen if one of the supplier's methods gonna try to access <code>Supplier</code> object via <code>supplier</code> member (that we, smart kids, know points to dead object... so sad... QQ). Guess. I believe it will die by realising he sees dead object first time in his life ;).</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