Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecial interaction between derived objects (i.e. mutiple dispatch)
    text
    copied!<p>So, I have a list of base class pointers:</p> <pre><code>list&lt;Base*&gt; stuff; </code></pre> <p>Then, at some point one of the objects will look through all other objects.</p> <pre><code>Base * obj = ...; // A pointer from the 'stuff'-list. for (list&lt;Base*&gt;::iterator it = stuff.begin(); it != stuff.end(); it++) { if (obj == *it) continue; // Problem scenario is here obj-&gt;interact(it); } </code></pre> <p>What I want to achieve is that depending on what derived type<code>obj</code> and <code>*it</code> are, they will interact differently with each other, i.e. <code>DerivedA</code> will destroy itself if it's interacting with <code>DerivedB</code>, but only if <code>DerivedB</code> has set the property <code>bool c = true;</code>. So something like:</p> <pre><code>struct Base { virtual void interact(Base * b); // is always called }; struct DerivedA : public Base { virtual void interact(Base * b){} // is never called virtual void interact(DerivedB * b) // is never called { if (b-&gt;c) delete this; } }; struct DerivedB : public Base { bool c = false; virtual void interact(Base * b){} // is never called virtual void interact(DerivedA * a) // is never called { c = true; } }; // and many many more Derived classes with many many more specific behaviors. </code></pre> <p>At compile time, they are both <code>Base</code>-pointers and will not be able to call each other and expect the type to magically appear. If this was a one way relation, i.e. I knew what type of one of them, I could use the <a href="http://en.wikipedia.org/wiki/Visitor_pattern" rel="nofollow noreferrer">Visitor pattern</a>. I believe I should use some kind of <a href="http://en.wikipedia.org/wiki/Mediator_pattern" rel="nofollow noreferrer">Mediator pattern</a> but can't really figure out how since the mediator too will hold <code>Base</code>-pointers and thus it won't make a difference.</p> <p>I haven't got a clue on how to continue... anyone?</p> <hr> <p><strong>Background:</strong></p> <p>I'm creating a game, this problem originates from the <code>Room</code> class who keeps track of it's contents, i.e. what <code>GameObject</code>s are currently in the room. </p> <p>Sometimes, an object moves (for example, the player). The room will then loop over all objects that are on the soon-to-be-moved-upon floor tile (the loop above) and will check if the objects will interact with eachother. </p> <p>For example, if it's a <code>Troll</code> the <code>Player</code> would want to hurt it. Or he would just like to hurt any <code>Character</code> (both <code>Troll</code> and <code>Player</code> are derived from <code>Character</code>) that originates from any another "team" (which can be accessed from the function <code>getAlignment()</code>, which all <code>Characters</code> implement).</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