Skribent: Hendrik Boom Dato: Til: dng Emne: Re: [DNG] Experiencing with GtkBuilder
On Mon, Nov 23, 2015 at 10:58:47AM -0500, Steve Litt wrote: > On Mon, 23 Nov 2015 06:45:54 -0500
> Hendrik Boom <hendrik@???> wrote:
>
>
> > There is a well-known hack in C wheereby you rely on C allocating
> > fields of structures independently of later fields.
> >
> > Thus with
> > struct foo{char c, int d, float e,}
> > and
> > struct bar{char c, int d,}
> >
> > (forgive me if I need semicolons here; I've used too many languages
> > in the alst year to remember the surface syntax)
> >
> > you can rely on c and d having the same offsets in both structures,
> > and so you can happily cast between pointers to foo and bar as long
> > as you don't mess with e.
> >
> > And, of course, you can have a pointer to a table of methods as the
> > first entry in all of these structures.
>
> Aren't C unions the "official" way to deal with these situations?
Like this?
union clum{struct foo foo, struct bar bar;}
or
struct u{vtable *vtabptr, union clum{struct foo foo, struct bar bar;}}
Thi could replace some casts, but you would no onger be open-ended in
that you'd have to define all the derived types in one place, the union, and
a user of your library would have a hard time defining a new derived type.