Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Clarify the operations you want to perform on LogicComponent. Assuming you are trying to achieve something like this: </p> <p>Step 1: Add a new entry to the map:</p> <pre><code>LogicComponent comp; EntityID id = 99; UnitInfos info = comp.add(id); </code></pre> <p>Step 2: Initialize the info:</p> <pre><code>info.x = 10.0; info.y = 11.0 // etc </code></pre> <p>Step 3: Get the info object again:</p> <pre><code>UnitInfos info2 = comp.instances[id]; // this is uninitialized. </code></pre> <p>Then, a few code comments are in order: </p> <p>The info object returned by comp.add is a COPY of the object you added to the map. By modifying it, you are not modifying what is in the map. </p> <p>The simplest fix is to create a map of pointers to the object instead of the object itself. </p> <pre><code>typedef std::map&lt;EntityID, T*&gt; pinstances_map; T * add(EntityID id) { T* t = new T(); instances[id] = t; return t; }; // initialize as UnitInfo *info = comp.add(id); info-&gt;x = 10.0; info-&gt;y = 11.0; // retrieve as UnitInfos *info = comp.instances[id]; </code></pre> <p>Also, do use an accessor method to get the mapped value, instead of exposing the map object as public. Make the instances variable protected, and add a public get() method.</p> <p>Edit: This code works fine for me:</p> <pre><code>#include &lt;map&gt; #include &lt;iostream&gt; using namespace std; template&lt;typename T&gt; class Component { public: typedef map&lt;long, T*&gt; pinstances_map; pinstances_map instances; T * add(long id) { T *t = new T(); instances[id] = t; return t; } }; struct UnitInfo { float x, y; }; class LogicComponent: public Component&lt;UnitInfo&gt; {}; int main() { LogicComponent comp; UnitInfo *info = comp.add(99); info-&gt;x = 10.0; info-&gt;y = 11.0; UnitInfo *info2 = comp.instances[99]; cout &lt;&lt; info2-&gt;x &lt;&lt; " " &lt;&lt; info2-&gt;y; return 0; } </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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