Name: Anonymous 2011-11-14 3:29
Has anyone here ever used this trick before?
And then say you have data types, A, B, and C, that each form some kind of linked structure with other instances of the same data type. But in this scenario, every instance is actually a member of a struct D, and you can get a pointer to the D instance from a pointer to one of its members.
So now the C data type can be implemented once, and used in many other data types, to provide the same linked structure for other data types. I assume the only other way to get this would be with inheritance, where D would have to inherit from C. But then D would also have to inherit from A and B, if one was to try to do this with them as well. In a language where multiple inheritance was not allowed, I guess you would just have to make a derived class for each of the A, B, C data types, and give each a reference to the master D instance that owned them. Then the conversion could be:
I'm trying to decide if this approach would be better than just using multiple inheritance in general. It seems more organized to me, but it might end up being more overhead. Thoughts?
#define OFFSET_IN_STRUCT(struct_name, member) \
&(((struct_name*)0)->member)
#define ADDRESS_OF_PARENT(struct_name, member, member_address) \
((member_address) - OFFSET_IN_STRUCT(struct_name, member))And then say you have data types, A, B, and C, that each form some kind of linked structure with other instances of the same data type. But in this scenario, every instance is actually a member of a struct D, and you can get a pointer to the D instance from a pointer to one of its members.
struct D {
struct A thingy;
struct B other_thingy;
struct C extra_thingy;
};
struct C* C_get_next_thingy(struct C*)
struct D* D_get_next_extra_thingy(struct D* self) {
return ADDRESS_OF_PARENT(
struct D,
extra_thingy,
C_get_next_thingy(&(self->extra_thingy)));
}So now the C data type can be implemented once, and used in many other data types, to provide the same linked structure for other data types. I assume the only other way to get this would be with inheritance, where D would have to inherit from C. But then D would also have to inherit from A and B, if one was to try to do this with them as well. In a language where multiple inheritance was not allowed, I guess you would just have to make a derived class for each of the A, B, C data types, and give each a reference to the master D instance that owned them. Then the conversion could be:
class D {
private A thingy;
private B other_thingy;
private C extra_thingy;
public D get_next_extra_thingy() {
return extra_thingy.get_next_thingy().get_D_reference();
}
}I'm trying to decide if this approach would be better than just using multiple inheritance in general. It seems more organized to me, but it might end up being more overhead. Thoughts?