what is the best way to share different objects between some classes in a generic manner?
For example class A can store an object o with a string as a key in a register and class B can access it using the key.
My first idea was to create a register (singleton) which has a hashtable as a member using a string as the key and a void pointer as the value. But there must be a better solution for this?
From your clarification:
That is to say,
A<int>is a class that storesintobjects by name, andA<std::set<float>>is a class that stores sets of floats by name. You can’t mix them. That’s in line with the basic C++ philosophy: the type oftheA.get("foo")is determined at compile time, not by what you put in at runtime.In C++, you can however “mix” multiple derived types, if you’d need this for your particular case. That’s a bit more complicated:
There’s some slight trickery here as
hashshould be the only owner of the copy, but it’s constructed outside and therefore needs to be moved it. (For extra-fancy, I could add aDerived&&overload that eliminates that copy too)