|
|
|
|
|
by repsilat
4199 days ago
|
|
One possible alternative is having your objects be members of "lists of slightly different type". For example, template <ptrdiff_t offset, class T>
struct node {
node<offset> *next, *prev;
T& operator*() { return *(this-offset); }
};
Maybe? It has been a while since I've written C++. I guess you have a big problem setting up your base class in a portable way, though, because the types of its member nodes depend on their offsets, and I don't think you can do that. class Foo {
int data;
node<offsetof(Foo, list1_elem), Foo> list1_elem;
node<offsetof(Foo, list2_elem), Foo> list2_elem;
};
:/ |
|
The base class option is, I think, best only for the case where you have only one kind of list you want the object to be part of.
Also your pointer should be cast to char before you do offsetof arithmetic on it.