I have a question regarding shared_ptrs and C ++ ownership:
I have a bunch of objects created on a heap. Each of them has a container that contains pointers to some of these objects, and sometimes even a pointer to the same object to which the container belongs. Since I read about the risks of using shared_ptr in such circumstances (roundness), I am thinking about how to do this best. Fortunately, there is a class that contains all objects (but not itself), so I thought that I give them ownership of them, so its container contains shared_ptrs, while the objects in question contain raw pointers . That is, destroying a class frees the memory allocated by the heap. Is this (also from a design point of view) a good decision to make?
Another step is to prevent objects from holding pointers, but rather unique identifiers. This would mean looking for the actual receipt of the object through its identifier, however, I think it will also reduce the critical dependencies between the objects. Is it preferable?
Hi,
Jena
source
share