...

View Full Version : C++ Destructors Not Calling



Spiegal
10-28-2008, 08:37 PM
I have written the following simple test program (transcribed from a book), but for some reason my destructors are not being called. What have I done wrong?


#include <iostream>

using namespace std;

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

class Mammal
{
public:
// constructors
Mammal();
Mammal(int age);
~Mammal();

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

// other methods
void Speak() const { cout << "Mammal sound!\n"; }
void Sleep() const { cout << "Mammal sleeping!\n"; }

protected:
int itsAge;
int itsWeight;
};

class Dog : public Mammal
{
public:
// constructors
Dog();
Dog(int age);
Dog(int age, int weight);
Dog(int age, BREED breed);
Dog(int age, int weight, BREED breed);
~Dog();

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

// other methods
void WagTail() const { cout << "Tail wagging . . .\n"; }
void BegForFood() const { cout << "Begging for food . . .\n"; }

protected:
BREED itsBreed;
};

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

Mammal::Mammal(int age):
itsAge(age), itsWeight(5)
{
cout << "Mammal(int) constructor . . ." << endl;
}

Mammal::~Mammal()
{
cout << "Mammal destructor . . ." << endl;
}

Dog::Dog():
Mammal(), itsBreed(GOLDEN)
{
cout << "Dog constructor . . ." << endl;
}

Dog::Dog(int age):
Mammal(age), itsBreed(GOLDEN)
{
cout << "Dog(int) constructor . . ." << endl;
}

Dog::Dog(int age, int weight):
Mammal(age), itsBreed(GOLDEN)
{
itsWeight = weight;
cout << "Dog(int, int) constructor . . ." << endl;
}

Dog::Dog(int age, int weight, BREED breed):
Mammal(age), itsBreed(breed)
{
itsWeight = weight;
cout << "Dog(int, int, BREED) constructor . . ." << endl;
}

Dog::Dog(int age, BREED breed):
Mammal(age), itsBreed(breed)
{
cout << "Dog(int, BREED) constructor . . ." << endl;
}

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


int main()
{
Dog Fido;
Dog Rover(5);
Dog Buster(6,8);
Dog Yorkie(3,GOLDEN);
Dog Dobbie(4,20,DOBERMAN);
Fido.Speak();
Rover.WagTail();
cout << "Yorkie is " << Yorkie.GetAge() << " years old." << endl;
cout << "Dobbie weighs " << Dobbie.GetWeight() << " pounds." << endl;
char response;
cin >> response;
return 0;
}

oracleguy
10-29-2008, 01:19 AM
Are you allocating the instances of the class on the stack or the heap? If it is the former you will not see the destructor fire until the function it is in terminates. So if you do this in the main() function, those messages will appear after the return statement is called in main().

Spiegal
10-29-2008, 06:21 PM
They're all on the stack. I expected the destructors after main ended (when return calls), but that didn't happen. My output was in the command prompt, and it called all the constructors and their appropriate couts as well as all of the test function couts, but not the destructors.

oracleguy
10-30-2008, 07:39 AM
They're all on the stack. I expected the destructors after main ended (when return calls), but that didn't happen. My output was in the command prompt, and it called all the constructors and their appropriate couts as well as all of the test function couts, but not the destructors.

Does your command window go away automatically when the program ends? If it does, thats why you aren't seeing the destructor messages. Try running it from an already open command window manually if you are starting it from inside an IDE.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum