Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2006
    Location
    Savannah, GA - Tech Pit of Despair
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C++ Destructors Not Calling

    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?

    Code:
    #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;
    }

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    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().
    OracleGuy

  • #3
    New Coder
    Join Date
    Apr 2006
    Location
    Savannah, GA - Tech Pit of Despair
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by Spiegal View Post
    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.
    OracleGuy


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •