classes

Mar 21, 2014 at 4:33pm
Ok well in my game I have created a class Critter, then a called
Critter* Mobs;
Mobs = new Critter[MAXMONSTERS]; // MAXMONSTERS = 5 for now

then I have where I fight some monster which the code always goes through
Mobs[I] to fight with but when I win im not sure how I remove that monster.
I tried Mobs[I].delete but gives me a error. some help would be great.

1
2
3
4
5
6
7
8
9
10
11
Critter* Mobs;
Mobs = new Critter[MAXMONSTERS];
// some function to attack
for(I = 0, I < MAXMONSTERS; ++I)

//then if Mobs[I].hps < 1 
cout << "u have killed the monster";
this is where I need to delete that mob


Mar 21, 2014 at 4:47pm
Use a standard container such as a vector rather than managing your own array.
Mar 21, 2014 at 4:54pm
I think I got it with a deconstructor command in class Critter called
~Critter().

so I was able to use Mobs[I].~Critter();

if this not how it should be done cause it creates leaks or what not please let me know thanks.
Mar 21, 2014 at 4:57pm
If you're deallocating memory in the deconstructor then there should be no memory leaks
Mar 21, 2014 at 5:00pm

so I was able to use Mobs[I].~Critter();


That won't remove the Critter from your array.

Last edited on Mar 21, 2014 at 5:00pm
Mar 21, 2014 at 5:01pm
im not sure, im just using the basic
Critter::~Critter() {}

ok it remove the critter from moving around my map. so where did it go when I called that?

or did it just cleared all the information in that Mobs[I]?

that would work too because I be needing to make another monster.
Last edited on Mar 21, 2014 at 5:07pm
Mar 21, 2014 at 5:13pm
man its ARRAY.......
though dynamically allotted.........
its single element cant be OUT of SCOPE at a time.
.
so Mobs[I] wouldn't be deleted.......alone ,whenever out of scope........
for deletion inside ~Critter () { delete []Mobs; } //will delete whole Mobs..

and soln=
use shifting to bring Mobs[I] to Mobs[0]
store its addrs. in temp ptr and use Mobs++;
U r done......but now delete that using temp ptr...........
.
.
.
.
.by the way.....let me see ur game.........#interested....hope would be nice.......
Last edited on Mar 21, 2014 at 5:21pm
Mar 21, 2014 at 5:57pm

im not sure, im just using the basic
Critter::~Critter() {}

Since the destructor is empty, that did nothing. You don't normally explicity call a destructor. The compiler generates a call to a destructor when an instance is deleted either via delete or when it goes out of scope.


so where did it go when I called that?

Nowhere. You didn't delete it.


or did it just cleared all the information in that Mobs[I]?

Again no. Your destructor did nothing.





Mar 21, 2014 at 6:11pm
well something has happened for me as when I called the destructor when I kill a Mob. I no longer see them. I have a map with 4 rooms(text based game) so when I goto room 1 and the Mobs room is the same then I see them. They can move around the rooms(works now without them getting lost), they can only move once every like 5 secs, if I attack them they cant move. I can attack them and kill them all(only 5). After I can move to all the 4 rooms and there is nothing in either.

so any ideas what happened to them?

did a little testing and it looks like the destructor has zero'd all the info in that Mob.
Last edited on Mar 21, 2014 at 6:48pm
Topic archived. No new replies allowed.