I have this class:
class GameData
{
public:
GameData();
~GameData();
vector<Bullet> bullets;
}
In the main program, I create a GameData pointer and allocate memory for it with new. I know with new, you should have to delete the data as well. My vector above is not a vector of pointers to Bullets, just Bullets themselves (should they be pointers?) In the ~GameData() function, what do I need to cleanup bullets? Should I make sure its empty, and if not clear it? what is the specific way to ‘delete’ it properly for good memory management. If my design is wrong, please let me know the proper way I should be handling this.
Thanks
No. It is done by the destructor of the vector which will be called automatically.
–
You don’t have to do that if you use some form of smart pointer, such as
std::unique_ptrorstd::shared_ptr. With smart pointers, you don’t have to delete it, as it is the responsibility of the smart pointer itself.Read the section Smart Pointers from here: