I'm trying to destroy an instantiation of a class using a destroyer. I also want to keep track of the numbers of class instances I created.
using namespace std;
class Asteroid
{
private:
double posx,posy;
static int i;
public:
Asteroid(double x,double y)
{
posx=x;
posy=y;
i++;
cout <<"Number of asteroids : "<<i<<endl;
}
~Asteroid()
{
i--;
cout <<"Number of asteroids : "<<i<<endl;
}
int pos()
{
cout <<"X : "<<this->posx<<" Y : "<<this->posy<<endl;
}
} ;
int Asteroid::i=0;
int main()
{
Asteroid a(1,2);
a.pos();
a.~Asteroid();
a.pos();
return 0;
}
Output:
Number of asteroids : 1
X : 1 Y : 2
Number of asteroids : 0
X : 1 Y : 2
Number of asteroids : -1
Desired output:
Number of asteroids : 1
X : 1 Y : 2
Number of asteroids : 0
X : 1 Y : 2 <-- I want this to be deleted from memory
Number of asteroids : -1 <-- 0 instead of a negative value