Skip to main content
gamedev.net
🔒 Locked

destructor problem

Started by Samaanc Jun 14, 2005 at 1:07 AM 5 replies 850+ views
Original Post
Samaanc
Samaanc
ok, i am having a bit of a problem executing the destructor in inhertence, i need some feed back! (questions in comments) header file (listing 12-3.h)

#include <iostream>
using namespace std;

enum BREED { GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB };

class Mammal
{
public:
	Mammal();
	~Mammal();

	int GetAge() const { return itsAge; }
	void SetAge(int age) { itsAge = age; }
	int GetWeight() const { return itsWeight; }
	void SetWeight(int weight) { itsWeight = weight; }

	void Speak() const { cout << "Mammal sound!\n"; }
	void Sleep() const { cout << "shhh. I'm sleeping.\n"; }
protected:
	int itsAge;
	int itsWeight;
};

class Dog : public Mammal
{
public:
	Dog();
	~Dog();

	BREED GetBreed() const { return itsBreed; }
	void SetBreed(BREED breed) { itsBreed = breed; }

	void WagTail() const { cout << "Tail wagging...\n"; }
	void BegForFood() const { cout << "Begging for food...\n"; }
private:
	BREED itsBreed;
};

Mammal::Mammal():
itsAge(1),
itsWeight(5)
{
	cout << "Mammal constructor...\n";
}

Dog::Dog():
itsBreed(GOLDEN)
{
	cout << "Dog constructor...\n";
}

Mammal::~Mammal() //how do i know this is executed?
{
	cout << "Mammal destructor...\n";
}

Dog::~Dog()
{
	 cout << "Dog destructor...\n";
}



and for source file, listing 12-3.cpp

#include "listing 12-3.h"

int main()
{
	Dog fido;
	fido.Speak();
	fido.WagTail();
	cout << "Fido is " << fido.GetAge() << " years old\n";
	system("PAUSE");
	return 0;
}//the destructor does not give me a message, does this mean i messed up?



any help will be greatly apprecated (i can't spell [depressed])!!! [repost]
gigabytes? HA! too small time! terrabytes, petabytes? fah, come on! exabytes, zettabytes? getting "warmer!" yottabytes, not even nonabytes! i'm talkin' doggabytes, thats my league!
cignox1
cignox1
Try with:
#include "listing 12-3.h"int main(){	Dog fido* = new Dog;	fido.Speak();	fido.WagTail();	cout << "Fido is " << fido.GetAge() << " years old\n";delete fido;	system("PAUSE");	return 0;}//the destructor does not give me a message, does this mean i messed up?


And add the keyword 'virtual' to the mammal destructor.
ascorbic
ascorbic
Agreed. Put "virtual" on your destructor on your base class and both will be called at the time of object destruction.
.
Samaanc
Samaanc
you guys are ablsolute gold, THANK YOU!! [smile]
gigabytes? HA! too small time! terrabytes, petabytes? fah, come on! exabytes, zettabytes? getting "warmer!" yottabytes, not even nonabytes! i'm talkin' doggabytes, thats my league!
Samaanc
Samaanc
Quote:
Original post by ascorbic
Agreed. Put "virtual" on your destructor on your base class and both will be called at the time of object destruction.


how do i place "virtual" on my base, give me an example
gigabytes? HA! too small time! terrabytes, petabytes? fah, come on! exabytes, zettabytes? getting "warmer!" yottabytes, not even nonabytes! i'm talkin' doggabytes, thats my league!
Gink
Gink
virtual Mammal::~Mammal(){



}
Emmanuel Deloget
Emmanuel Deloget
(edit: extended)

Quote:
Original post by Gink
virtual Mammal::~Mammal(){
}

Not at all :)

Form 1
// hclass Mammal{public:  virtual ~Mammal();};// cpp:Mammal::~Mammal(){}


Form 2
// hclass Mammal{public:  virtual ~Mammal()  {  }};


Form 1 is prefered, unless you don't want to add a cpp file.

cignox1 's code has also some small syntax errors:

#include "listing 12-3.h"int main(){	// Dog fido* = new Dog; // error	Dog *fido = new Dog;	fido->Speak(); // using -> instead of .	fido->WagTail(); // using -> instead of .	cout << "Fido is " << fido->GetAge() << " years old\n";	delete fido;	system("PAUSE");	return 0;}//the destructor does not give me a message, does this mean i messed up?


Moreover, it will only make sure that the messages will be displayed before PAUSE command is issued to the system (side note: this PAUSE system command is really auwful). You can achieve the same results by using this

#include "listing 12-3.h"int main(){	{		Dog fido;		fido.Speak(); // using -> instead of .		fido.WagTail(); // using -> instead of .		cout << "Fido is " << fido.GetAge() << " years old\n";	}	system("PAUSE");	return 0;}


The real problem is really the missing "virtual" keyword. Without this keyword, only the Dog destructor is called. Both Mammal and Dof should define their destructor as virtual. In fact, unless you are really sure that you class won't be inherited, it is a good idea to make the destructor virtual.

HTH,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.