Note: I wrote this question in a way that made it seem I'm mainly concerned with the memory usage and ways to optimise this. Rather, my original intention (and my current intention, upon revisiting this question) is to ask for solutions to the maintainability problem: the fact that one has to make sure that a corresponding pair of key (in the associative container) and field (in the object itself) for the name/ID of the object stay in sync.
I have often encountered a situation in which users of a program (not necessarily human, just an external agent) need to be able to reference objects of a class with a certain "external" identifier or name, in order to create, access, modify or delete them. A couple of examples:
- a command-line application where end-users refer to specific instances by name, e.g. a calculator that deals with geometrical objects
- a GUI with a dynamically created drop-down list where each item corresponds to an instance
This identifier could be an integer, for example, or a string. We'll suppose it's a string for the sake of the argument.
Where should these IDs be stored?
Let me clarify. In order for the program to "link" a certain identifier to the corresponding actual object, it needs to have some sort of associative container with IDs as keys and their corresponding objects as values.
Now, suppose some method of this class needs access to the identifier; e.g. a
prettyPrint method that also prints the object's name. In this case, should the identifier be a field of the class? This seems like the obvious solution. But the problem is that now the identifiers are duplicated: as keys in the associative container, and as fields in each instance. That would imply duplicate "management tasks"—i.e., when adding a new object to the associative container, one would need to specify the identifier as both key and field. It would also entail slightly more memory usage, but this is the least significant of the two problems.
Is there a general approach to this problem? (Or am I fussing over nothing?)
One approach would be to have the associative container keep references to the ID field as keys. But I don't know how to do that, at least in a language like C++.