Why can't I inherit (extend) structs?

Johan Granberg lijat.meREM at OVEgmail.com
Mon Oct 16 22:09:43 PDT 2006


Bill Baxter wrote:
> Derek Parnell wrote:
>> On Mon, 16 Oct 2006 18:49:20 +0200, Johan Granberg wrote:
>>
>>> Derek Parnell wrote:
>>>> And I'd go so far as to say that the term 'inheritance' is a bit 
>>>> strong.
>>>> Maybe just 'derivation' as we would be deriving a new struct definition
>>>> from existing definitions, but there is no implied linkage between 
>>>> them at
>>>> runtime. 
>>> If it is just derived from that is wanted wouldn't it bee easier with 
>>> a syntax like this.
>>>
>>> struct foo
>>> {
>>>     int b;
>>>     int c;
>>> }
>>> struct bar
>>> {
>>>     int a
>>>     include foo;
>>>     int d
>>> }
>>>
>>> the struct bar would now have four fields (a,b,c,d)
>>>
>>> this way their is no way to misunderstand it for inheritance and if 
>>> one want a foo* one can always take the addres of the first element.
>>
>> Hey ... not bad.
> 
> By "first element" you mean "first foo element in bar"?
> 
> Isn't that just a mixin without the namespace?
> 
> Anyway, I thought the point of this was so that you could pass a bar* to 
> a function that takes a foo*.  If foo is jammed in somewhere in the 
> middle of bar then that's not so straightforward.  I guess the compiler 
> could automatically offset the bar* to its foo part if it sees the bar* 
> is being used in a foo* context.  But that seems kinda tricky to get right.
> 
> --bb

I you put foo first in bar that would work otherwise you would have to 
offset the address yourself.

Yes it is much like a mixin the difference is that you are mixing in a 
struct instead of a template.



More information about the Digitalmars-d-learn mailing list