On Mon, Mar 10, 2008 at 9:18 PM, Giel van Schijndel <[EMAIL PROTECTED]> wrote:
>  > You will find that passing along a field belonging to
>  > an object (psDroid->baseObject) and passing along an enlarged version
>  > of an object ((BASE_OBJECT *)psDroid) are two very different things,
>  > and to code to handle it has to be written differently. Eg BASE_OBJECT
>  > *psTile->psObject cannot be cast back to DROID*, you need a pointer
>  > back to the parent to do that, either by a void pointer or a union -
>  > either way it gets ugly, IMHO.
>
>  Actually in this example case you can just downcast baseObj to a DROID*
>  without any problem (provided you checked the type properly), as both
>  baseObj and droidObj start at the same address.

Just to nitpick a little bit. You cannot downcast, since the memory
address points to the child object. Take this example:

void objBar(BASE_OBJECT *psObj)
{
      doBarify(psObj);
      if (psObj->type == OBJ_DROID)
      {
           doBarForDroidToo((DROID *)psObj);
      }
}

void droidBar(DROID *psDroid)
{
      someOtherBarFunc(psDroid, SOME_SILLY_CONSTANT);
      objBar((BASE_OBJECT *)psDroid);
}

If you would pass on psDroid->baseObj to objBar, then the original
memory address would be lost, unless you recorded it somewhere in
baseObj, breaking the abstraction. This reference to the higher level
object from a lower level one is often used in the code base. I think
the neatest way around that would be to keep a (object)->parent field
in every object, eg psDroid->baseObj->parent. This would be eerily
similar to how the tagfile structures work, too ;-)

Not that I would recommend doing this for reasons previously mentioned.

  - Per

_______________________________________________
Warzone-dev mailing list
Warzone-dev@gna.org
https://mail.gna.org/listinfo/warzone-dev

Reply via email to