I implement the "clone" function for graphing objects in C ++, and part of the problem is to make sure that if there are two pointers to the same object, it is not cloned twice. I did this by saving map<void*, void*> , which saves the original object as a key and the cloned version as a value. When cloning an object, I use the template function to check if the object is on the map - if so, I return it using static_cast<T*> , otherwise I clone it and save the original and clone on the map with implicit conversion to void* .
The problem with this scheme is that if an object is mentioned in two places by different types (for example, by interface against a specific type), casting to void* may not lead to the same value. This means that the object is cloned twice.
I looked on the Internet for existing solutions and realized that Boost.Serialization had to solve the same problem. But after trawling through its source, I could not find the part that actually tracks pointers to objects.
Can someone help by suggesting a design that works, or by pointing to the part of the Boost code that does this?
source share