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, in order to create, modify and delete them. 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, providing thus logarithmic access time.
Now, suppose some method of this class needs access to the identifier. 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 double the storage necessities and 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).
Is there a general way to overcome this problem? (Or am I fussing over nothing?) One alternative would be to have the associative container keep references to the ID field as keys. But I don't know how to do that.
For example, in C++, consider the class
class MyClass {
public:
std::string ID;
MyClass(string id) : ID(id) {}
}
Then the straightforward approach would be to store a std::map<std::string, MyClass>. I tried to make use of std::reference_wrapper to store references to the IDs, but then the order of the map doesn't work since operator< isn't defined on reference_wrappers:
int main() {
std::map<reference_wrapper<std::string>, MyClass> myInstances; // error
MyClass obj("Bob");
myInstances.insert({obj.ID, obj});
}
Is there a way to do this? Or is it unnecessarily complicated?
In Python, I believe this
class MyClass:
def __init__(self, ID: str):
self.ID = ID
myInstances = {}
obj = MyClass("Bob")
myInstances[obj.ID] = obj
would work, since both the myInstances[obj.ID] identifier (as in Python object identifier) and the self.ID identifier would point to the same string.