Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this statement calling this destructor?
    primarykey
    data
    text
    <p>I have two template classes called arrContainer and pArrContainer. arrContainer acts much like a vector class does, basically a wrapper for a dynamic array, written to simplify memory management for dynamic arrays, and pArrContainer works almost the same as arrContainer, except that it is used for dynamic arrays of pointers. the idea is that it will clear the memory it uses for the dynamic array, as well as clear the memory of the pointers in the array.</p> <p>Furthermore, I have a class Board, representing a board in a board game. Its fundamental member it uses to do this is arrC_board, a arrContainer&lt; pArrContainer&lt; Tile&lt; Piece> > > Where Tile is the template class used to represent each tile on the board, and Piece is a class used to Represent the individual pieces on the board.</p> <p>Tile's internal structure is depicted here. Since it is used for Piece, tmp will be Piece:</p> <pre><code>template &lt;typename tmp&gt; class Tile { protected: ... tmp* tmp_occupant; public: Tile(); ~Tile(); tmp* getOccupant() const {return tmp_occupant;} ... }; </code></pre> <p>Board's internal structure is depicted here:</p> <pre><code>template &lt;typename tmp&gt; class Board { protected: arrContainer &lt; pArrContainer &lt; Tile &lt; tmp &gt; &gt; &gt; arrC_board; int i_rows; int i_columns; public: //Creates a 0x0 board (No elements!) Board(); //Creates a IxJ board (Row x Column) Board(int I, int J); //Deletes the memory inside of all the tiles as well. ~Board(); tmp* getElement(int I, int J) const; ... }; </code></pre> <p>But in when I call Board's getElement function:</p> <pre><code>template &lt;typename tmp&gt; tmp* Board&lt;tmp&gt;::getElement(int I, int J) const { if (I &gt;= 0 &amp;&amp; J &gt;= 0 &amp;&amp; I &lt; i_rows &amp;&amp; J &lt; i_columns) { Piece* occupant = arrC_board[I][J]-&gt;getOccupant(); return occupant; } else .... } </code></pre> <p>and get to the <code>Piece* occupant = arrC_board[I][J]-&gt;getOccupant();</code> part, <code>getOccupant()</code> returns the proper value, but pArrContainer's destructor is called before the value is actually assigned to <code>occupant</code>.</p> <p>Why is this?</p> <p>For reference, the following function bodies have been included:</p> <p>pArrContainer's overloaded subscript operator:</p> <pre><code>template &lt;typename tmp&gt; tmp* pArrContainer&lt;tmp&gt;::operator[] (const int index) { if (index &gt;= size) { ...//this part isnt executed } else return heldArray[index]; } </code></pre> <p>arrContainer's subscript operator looks the same as pArrContainer's. Tile's <code>getOccupant()</code> function:</p> <pre><code>tmp* getOccupant() const {return tmp_occupant;} </code></pre> <p>I apologize for the amount of reading needed for this question.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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