Per Inge Mathisen schreef:
> On Mon, Mar 10, 2008 at 2:07 PM, Dennis Schridde <[EMAIL PROTECTED]> wrote:
>>  BASE_OBJECT, DROID and STRUCTURE have been ugly since a long time, we all 
>> know
>>  that.
> 
> But it is amazing how quickly I got used to it ;-)

Is that a good thing? :-P

>>  That system of "inheritance" is responsible for a lot of warnings and, due 
>> to
>>  its heavy use, probably also for a lot of bad code (aka "wrong" assembly).
> 
> I can't think of a single bug caused by wrong inheritance, actually.
> What warnings?

I can easily imagine errors like this:
A function takes a BASE_OBJECT* parameter then casts it to DROID* while
it was really passed a STRUCTURE*. This wouldn't be caught by the
compiler, though this could just as easy occur when C++ was used (and
dynamic_cast would _not_ be used).

>>  And of course it looks ugly... ;)
> 
> It can be mitigated by making wrapper methods for inherited versions.
> Eg you can have objBar(BASE_OBJECT *psObj) and droidBar(DROID
> *psDroid) which is just an inline call to objBar. You could also use
> void pointer parameters, but then you'd lose all type safety.

Please, no, IMO that approach would only serve to make reading of the
code more difficult (due to the larger amount of function names you need
to keep on your "brain-stack"). IMO the only solution to this would be a
language that supports either function overloading, or inheritance at a
language level. Apart from those two options I think the current
situation is good enough (though admittedly, slightly error-prone).

>>  I propose we fix that by going a step towards proper inheritance, i.e.:
>>  struct BASE_OBJECT {
>>         /* ... */
>>  };
>>  struct DROID {
>>         BASE_OBJECT baseObj; /* Variants: "base", "super", ... */
>>  };
>>
>>  That would probably cut down the number of casts by 90%. ;)
> 
> It is a nicer way of handling it. However, that is an enormous change
> to the codebase. 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.

> I am quite sure we don't want to go there.

That said, I have to agree with this, it would mean a _huge_ change of
the codebase (probably larger than switching to a C++ inheritance
scheme) and (IMO) wouldn't really give any benefit (aside from making it
look less ugly).

> (PS Also, if we ever go for C++, I think it would be easier to move
> from the current inheritance model to real inheritance, than from the
> alternatives.)

Exactly! And we wouldn't want to close the door to C++ now would we ;-)

-- 
Giel

Attachment: signature.asc
Description: OpenPGP digital signature

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

Reply via email to