struct inheritance

kris foo at bar.com
Fri Aug 31 14:53:29 PDT 2007


Regan Heath wrote:
> kris wrote:
[snip]
>> Briefy:
>>
>> ok, S /extends/ M :)
> 
> <g>
> 
>> The ordering becomes an issue when you have a pointer to S, and wish 
>> to use it as a pointer to an M. That's why, in C, the super-struct is 
>> manually inserted as the first struct member. If you manually insert 
>> the super-struct as the second or third member then, obviously, your 
>> pointer is not convertible
> 
> Yep, exactly.  In this case you definately want S to extend M and in 
> this exact manner.  However, I wonder if there are cases where you want 
> a different layout in memory.  If so, the syntax must support both options.
> 
> Regan

If you're not interested in 'extending' per se, but prefer composition, 
then one will always be able to do this:

struct S
{
   int foo, bar;
   M m;
   OtherStruct other;
   char[] wumpus;
}



More information about the Digitalmars-d mailing list