Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

indexing out from a struct member

Name: Anonymous 2011-11-14 3:29

Has anyone here ever used this trick before?


#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?

Name: Anonymous 2011-11-14 15:48

Hum didn't know there was a function for this but used this method before yes.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List