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 | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
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

2nd Aug 2016, 12:04 AM
The Klespyrian
The Klespyrian - avatar
4 ответов
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.
2nd Aug 2016, 2:50 PM
Bernhard .Speicher
Bernhard .Speicher - avatar
0
So when an enemy's health was to reach 0 I should call this delete function?
2nd Aug 2016, 2:53 PM
The Klespyrian
The Klespyrian - avatar
0
depends on how youre storing the enemies. if you allocated the space for them dynamically using new, them yes.
2nd Aug 2016, 2:56 PM
Bernhard .Speicher
Bernhard .Speicher - avatar
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?
2nd Aug 2016, 3:05 PM
The Klespyrian
The Klespyrian - avatar