A long time ago I had a discussion with Frank Taylor regarding the "best way" to maintain links between objects. I suggested having fields with pointers to objects, while Frank preferred storing numerical IDs and looking objects up via their ID in arrays. One of his major points was that serialization of objects (storing them on disk and reading them back again) would be very difficult without IDs. I've recently implemented a solution I read about which elegantly solves the problem without resorting to IDs:
When saving objects, store each object's pointer (as a Handle()) and each pointer field simply as a pointer (as a Handle().) When loading objects back in, keep a hash map of old_ptrs to new_ptrs, adding an entry every time you load an object. When you're finished loading all the objects, go back over them and translate their pointer fields using the hash map you built. Voila: the objects now point to each other again!
This works because pointers are guaranteed to be unique. It's even easier to pull off in C++ because you can store a list of pointers to all the pointers you're loading, then loop over them afterwards - although a std::vector<void * *> gives me a headache.
When saving objects, store each object's pointer (as a Handle()) and each pointer field simply as a pointer (as a Handle().) When loading objects back in, keep a hash map of old_ptrs to new_ptrs, adding an entry every time you load an object. When you're finished loading all the objects, go back over them and translate their pointer fields using the hash map you built. Voila: the objects now point to each other again!
This works because pointers are guaranteed to be unique. It's even easier to pull off in C++ because you can store a list of pointers to all the pointers you're loading, then loop over them afterwards - although a std::vector<void * *> gives me a headache.