Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 1
    This question is better asked at Stack Overflow. Commented Sep 6, 2020 at 0:39
  • A very easy way is just make a copy of the Player object (and with all the data inside, i.e. "deep copy"), then throw away the original instance. Whether this is "elegant" or "nice" depends on other details in your architecture. Commented Sep 6, 2020 at 0:41
  • You don't have to modify unique_ptr itself when implementing your idea. Put the autoCleanup flag inside your user-defined type (GameObject), and then your actual cleanup code should read: vector<GameObject*> forKeeping; vector<GameObject*> forDiscarding; for (GameObject* obj : gameObjects) { if (obj->autoCleanup) forDiscarding.push_back(obj); else forKeeping.push_back(obj); } Instead of moving things into a forDiscarding, you can also delete them right away. Commented Sep 6, 2020 at 0:45
  • 3
    " it would be needlessly slow to reference count those with shared_ptr" - frankly, whenever I read statements like this about hypthetical performance, chances are high they are unjustified, superstitious nonsense. Go, try it out, and when this simple solution does not match you performance requirements, then think about a more complex solution. Commented Sep 6, 2020 at 6:34
  • 2
    Apart from using smart pointers being a good idea in general: Throwing owning pointers and non-owning pointers into the same vector is a very bad idea. You will mess this up at some point. That’s where I would start. That vector should hold either players or other GameObjects, but not both. Commented Sep 6, 2020 at 10:56