On 09/09/2012 21:05, Marvin Humphrey wrote:
On Sun, Sep 9, 2012 at 4:00 AM, Nick Wellnhofer <[email protected]> wrote:
void
Sub_method(void *self) {
Sub_MEMBERS *members = Sub_get_members(self);
members->sub_num = 1;
members->sub_float = 1.0;
// Only safe if Base is in the same parcel
Base_MEMBERS *base_members = Base_get_members(self);
base_members->base_num = 1;
base_members->base_float = 1.0;
}
I'd originally thought to avoid this approach because of the extra line of
code, but perhaps it's not so bad after all. Plus, there's an idiom for
single line access;
void
Foo_set_num(Foo *self, int num) {
Foo_MEMBERS(self)->num = num;
}
Another solution is to provide the members pointer as an argument to
every method:
void
Foo_set_num(Foo *self, Foo_MEMBERS *members, int num) {
members->num = num;
}
The offset would then be applied in the method wrappers (Foo_Set_Num
without the members argument). The same technique as in Marvin's initial
approach could be used to store method and members offsets at a single
memory location. This would give a slight performance benefit (mainly on
x86, 32 bits) by avoiding a global variable lookup.
But I'm not convinced it's worth the added complexity. I still prefer to
make the lookup of the members struct explicit.
Nick