0
Can someone explain how a destructor would work if I was to kill an enemy in a text-based game. Specifically the syntax I would use to make sure it was destructed once it's health reached 0
4 Réponses
0
you dont need to do anything, destructors get called automatically when the object gets deleted. if youre not using something like std::vector from the container classes to store objects but are instead using raw pointers you just have to call delete. one other way to handle this is to store a bool in the class that keeps track of dead/alive, which also lets you reuse the allocated space for the next enemy.
0
So when an enemy's health was to reach 0 I should call this delete function?
0
depends on how youre storing the enemies. if you allocated the space for them dynamically using new, them yes.
0
Would it be plausible for me to say have an enemy class with base stats, have one constant instance of that class that I just add modifiers to between each battle depending on whatever factors I need (level, additional stats, etc) or would creating / destroying enemies be a better option?