Reputation: 313
Here's the problem: I am making a game using SFML, and I wanted to have a vector of sf::Drawable* so I could simply do windowManager.add(randomGameSprite), but the problem I have is that I want to be able to have it so I can do delete randomGameSprite without having to manually remove the pointer from the window. Is there some way to have it check to see if the object exists before trying to draw it?
I am using C++11, but the smart pointers haven't been much help in this from what I've tried. I tried using std::shared_ptr, but it keeps the drawables alive.
Upvotes: 3
Views: 235
Reputation: 500673
You could store weak_ptr<T>
in your vector. A weak pointer will not keep the object alive: if all shared pointers to the object go out of scope, the weak pointer will automatically expire.
Upvotes: 8