I'm writing in C++, but this problem applies to any language without GC and even to languages with a GC as well.
I have a structure in memory in which I create/add objects. The structure takes ownership of those objects. I should never need to use an object after it's removed from the structure.
When I first implemented this data structure, it seemed natural to use an ID/key/name/handle for the objects stored in it. I'm using it like this:
id1 = structure.addObj(new Square());
id2 = structure.addObj(new Square());
id3 = structure.addObj(new Circle());
obj3 = structure.getObj(id3);
obj3.addFriend( id1 );
obj3.addFriend( id2 );
idMax = structure.findObjWithMostFriends();
objMax = structure.getObj(idMax);
print(objMax.name);
After using it for a while, I'm thinking that it would be better to forget about the IDs and always use references to the objects instead. This way I wouldn't need to pass a reference to the structure around every time.
I'm thinking about refactoring everything to only use references but I'm afraid of regretting it. I'd like to know more about what are the pros and cons of using IDs to decide whether I should proceed.
Memory details:
The objects are allocated on the heap and their address never changes.
The structure deallocates those objects when they're removed (they could be released to the caller instead, but I don't need this at the moment).
I'm not supposed to ever use objects that don't belong to the structure. If my program is correct, I should never end up with a dangling ID or pointer. But it could happen if the program has bugs.
What are your experiences switching from IDs to references for similar problems? Which solution should I use?
obj3.addFriend()
). Every referenced node should be in the same structure as well. The structure maintain ownership of all nodes. . . I'm not trying to gain anything by using IDs to identify the objects in the structure: it just felt natural while designing it. I'd like to switch to only using pointers/references, but I fear that it might create problems. I posted this question in order to learn from other more experienced programmers who might have run into the same problem.obj3.addFriend( id1 );
That indicates thatCircle
knows about structure and id's. That's evil and wrong. Give me a system that doesn't need the ID'd objects to know about it.