Jump to content
  • Advertisement
Sign in to follow this  

destructor problem

This topic is 4725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
(edit: extended)

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

Not at all :)

Form 1

// h
class Mammal
{
public:
virtual ~Mammal();
};

// cpp:
Mammal::~Mammal()
{
}


Form 2

// h
class 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,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!