Note that there are some explanatory texts on larger screens.

plurals
  1. POc++ with pointers losing pointer reference
    text
    copied!<p>I got a class that is described below</p> <pre><code>class Investment { private: void init(BWAPI::UnitType type1, BWAPI::UpgradeType type2, BWAPI::TechType type3, int numOfItems); UpgradeType upgradeType; TechType techType; UnitType unitType; </code></pre> <p>and my init method is like that</p> <pre><code>void Investment::init(BWAPI::UnitType type1, BWAPI::UpgradeType type2, BWAPI::TechType type3, int numOfItems) { if (type1 != NULL) { this-&gt;unitType = type1; this-&gt;type = type1.isBuilding() ? BUILDING_TYPE : UNIT_TYPE; } else if (type2 != NULL) { this-&gt;upgradeType = type2; this-&gt;type = UPGRADE_TYPE; } else if (type3 != NULL) { this-&gt;techType = type3; this-&gt;type = TECH_TYPE; } this-&gt;numOfItems = numOfItems; } </code></pre> <p>and I get my item (that can be only one of the three possible types) this way</p> <pre><code>const void* Investment::getItem() const { if (unitType != NULL) return &amp;unitType; else if (upgradeType != NULL) return &amp;upgradeType; else if (techType != NULL) return &amp;techType; return NULL; } </code></pre> <p>but what happens when I get the item using <code>UnitType* type = (UnitType*) investment-&gt;getItem();</code> the pointer lose its value</p> <p>when I call <code>type-&gt;getName().c_str();</code> it returns an empty string</p> <p>to get investment I call a method of a Stack </p> <pre><code>// I have a stack of type std::vector&lt;T*&gt; stack; T* getNext() { clear(); for (int i = 0, leni = stack.size(); i &lt; leni; i++) { T* t = stack.at(i); if (!t-&gt;isDone() &amp;&amp; !t-&gt;isCanceled()) return t; } return NULL; } </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