Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, check the <strong>union construct</strong> suggested by mipadi; that's a very readable and efficient way of <strong>dealing with polymorphism</strong>.</p> <p>Closer to your snippet/question, at a quick glance, I don't see the need/use for the double indirection introduced by pointer-to-pointers. The whole logic would work the same if the arguments to xxxx_fire() methods were [direct] pointers to xxxx objects (and if the typecast etc. in the rest of the logic were to follow accordingly.</p> <p><strong>Pointers to pointers are useful when the value of the intermediate pointer may be changed at some point</strong>. For example if the underlying object is moved, or if it replace by a different object altogether (say a better-equipped ship part of a new level in game etc...)</p> <p><strong>Edit</strong>: (on the <strong>use of double indirection to manage "fleets" of objects which may be deallocated</strong>.<br> Responding to your comment, do <em>not</em> refactor so that the objects are <em>not</em> de-allocated (from memory) when they get killed/detroyed (as part of the game). <em>Instead</em>, look into something like the following, as this is indeed an example where the pointer-to-pointer construct helps a lot. Here's how it could work:</p> <ul> <li>Upon game (or level) initialization, allocate an array <em>of pointers</em> big enough to contain as many pointers as the total number of objects the game may allocate, over time. Initialize all its values to NULL.</li> <li>Introduce an int value index, which indicates the location of the next available (=unused so far) pointer in this array.</li> <li>When a new object (UFO, Ship or what have you) gets created, four things happen:<br> <ul> <li>new memory is allocated for the object per se</li> <li>the address of this new memory is stored in the object pointer array (at the location indicated by the index)</li> <li>the index gets incremented</li> <li>the "world" only knows this object by way of the double indirection</li> </ul></li> <li>when an object gets destroyed two things happen <ul> <li>the memory is freed</li> <li>the pointer in the array is set to null</li> </ul></li> <li>when accessing any objects the program does three things <ul> <li>first dereference (once) the pointer-to-pointer</li> <li>check if this is null (if so this indicate the object doesn't exist anymore, the logic may decide to remove this reference from wherever it stored it, as so to not try again, but this is of course optional).</li> <li>access the actual object by dereferencing the intermediate pointer (if it isn't NULL)</li> </ul></li> </ul> <p>In insight, a short snippet in C language may have been more explicit; sorry I described this in words...</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