Стек на базе класса

Модераторы: Hawk, Romeo, Absurd, DeeJayC, WinMain

Ответить
zhenek
Сообщения: 4
Зарегистрирован: 20 мар 2009, 18:01

Вопрос следующий, как реализовать деструктор?

Код: Выделить всё

Bullet :: ~Bullet()
{
  while( !IsEmpty(this) )
	{
		Pop(this);
	}
  delete (this);
}
bool Bullet::IsEmpty(Bullet * const bul)
{
	if(!(bul))
		{
			return true;
		}
	if(!bul->top)
		{
			return true;
		}
	return false;
}
void Bullet::Pop(Bullet * bul)
{	
	Bullet * iter;

	iter = bul->top;

	bul->top = bul->top->last;

	if ( iter != NULL )
	{
		delete iter;
	}
}
и сам класс

Код: Выделить всё

class Bullet
{
private:
	Bullet * last;
	Bullet * top;
	double mass;
	int speed;
	int thick_arm;
public:
	Bullet ();
	~Bullet ();
	void Push (double a, int b, int c);
	void Print ();
	bool IsEmpty (Bullet * bul);
	void Pop (Bullet * bul);
};
--------------------------------------------------------------------------------
Добавлено сообщение
--------------------------------------------------------------------------------
ведь в деструкторе указатель получается константный, в pop не изменишь
--------------------------------------------------------------------------------
Добавлено сообщение
--------------------------------------------------------------------------------
Вот весь код, буду благодарен, если кто-нибудь протестит и подскажет, как исправить ошибку

bullet.h

Код: Выделить всё

#ifndef bullet_h
#define bullet_h
class Bullet
{
private:
	Bullet * last;
	Bullet * top;
	double mass;
	int speed;
	int thick_arm;
public:
	Bullet ();
	~Bullet ();
	void Push (double a, int b, int c);
	void Print ();
	bool IsEmpty ();
	void Pop ();
};
#endif 
main.cpp

Код: Выделить всё

#include "bullet.h"
#include <iostream>
using namespace std;
int main ()
{
	int magazine;
	int code;
	int amount;
	Bullet AK74;
	cout << "Enter amount of cartridge in magazine, no more 35\n";
	cin >> magazine;
	if (magazine <= 35)
	{
	for( int i=0; i<magazine; i++)
	{
        cout << "tracer bullet(1),usual bullet(2),armour-piercing(3)\n";
		cin >> code;
		switch (code)
		{
		case 1: AK74.Push(10.3,883,6);break;
		case 2: AK74.Push(10.5,880,5);break;
		case 3: AK74.Push(10.7,880,16);break;
		}
	}
	}
	else
	{
		cout<<"Incorrect amount!\n";
	}
	cout << "Enter amount of cartridge for fire\n";
	cin >> amount;
	if (amount <= magazine )
	{
		AK74.Print();
	}
	else
	{
		"Incorrect amount!\n";
	}
	system ("PAUSE");
	return 0;
}
bullet.cpp

Код: Выделить всё

#include "bullet.h"
#include <iostream>
using namespace std;

Bullet ::Bullet()
{ 
	mass = 0;
	speed = 0; 
	thick_arm = 0; 
	top = NULL; 
	last = NULL;
}
Bullet :: ~Bullet()
{
  while( !IsEmpty() )
	{
		Pop();
	}
  delete (this);
}
void Bullet :: Push(double a, int b, int c)
{
	Bullet * newit;
    newit = (Bullet *)malloc(sizeof(class Bullet));
	newit->last = top;
	newit->mass = a;
	newit->speed = b;
	newit->thick_arm = c;
	top = newit;
}
void Bullet :: Print ()
{
	Bullet * newit = this->top;
	while(newit != NULL)
	{
		cout << "Mass = " << newit->mass<<"\n";
		cout << "Speed = " << newit->thick_arm <<"\n";
		cout << "Thickness = " << newit->thick_arm <<"\n";
		cout<<endl;
		newit = newit->last;
	}
}
bool Bullet::IsEmpty()
{
	if(!(this))
		{
			return true;
		}
	if(!top)
		{
			return true;
		}
	return false;
}
void Bullet :: Pop()
{	
	Bullet * iter = this->top;

	this->top = iter->last;

	if ( iter != NULL )
	{
		delete iter;
	}
}
Ответить