stupid compiler

This is a discussion on stupid compiler within the C++ Programming forums, part of the General Programming Boards category; Works for me with Dev-C++, but it doesn't even output "Dog Destructor Called" once or twice....

  1. #16
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Works for me with Dev-C++, but it doesn't even output "Dog Destructor Called" once or twice.

  2. #17
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    That's true jlou, but my original point still stands. It is best to over-ride equals to ensure everything in your class gets copied over correctly.

  3. #18
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Added this code and it works

    fido.~Dog();
    rover.~Dog();

    So it now looks like:

    Code:
    #include <iostream>
    using namespace std;
    
    class Dog {
    private:
        int age;
        int weight;
    public:
        Dog();      //Constructor
        ~Dog();    //Destructor
        void setAge(int age);
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    };
    
    Dog::Dog()
    {
        age = 0;
        weight = 0;
        cout << "Dog Constructor Called" << endl;
    }
    
    Dog::~Dog()
    {
        cout << "Dog Destructor Called" << endl;
    }
    
    void Dog::setAge(int age)
    {
        this->age = age;
    }
    
    int Dog::getAge()
    {
        return age;
    }
    
    void Dog::setWeight(int weight)
    {
        this->weight = weight;
    }
    
    int Dog::getWeight()
    {
        return weight;
    }
    
    void Dog::speak()
    {
        cout << "BARK!!" << endl;
    }
    
    int main()
    {
        Dog fido;
        Dog rover;
    
        cout << "Rover is " << rover.getAge() << " years old." << endl;
        cout << "He weighs " << rover.getWeight() << " lbs." << endl;
        cout << endl;
    
        cout << "Updating Rover's Age and Weight" << endl;
        rover.setAge(1);
        rover.setWeight(10);
    
        cout << "Rover is " << rover.getAge() << " years old." << endl;
        cout << "He weighs " << rover.getWeight() << " lbs." << endl;
        cout << endl;
    
        cout << "Fido is " << fido.getAge() << " years old." << endl;
        cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    
        cout << "Setting Fido to be the same as Rover" << endl;
        fido = rover;
    
        cout << "Fido is " << fido.getAge() << " years old." << endl;
        cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    
        rover.speak();
        fido.speak();
        fido.~Dog();
        rover.~Dog();
        
        cin.get();
    
        return 0;
    }

  4. #19
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Since the destructors are called at the very end of your program, it is possible you just aren't seeing the output. Try putting the objects inside a smaller scope. The destructors are called as soon as the scope ends. In the original code, that is the end of the main function. In the code below, the extra scope (created with the extra braces {,}) allows the destructors to be called before main ends.
    Code:
    #include <iostream>
    using namespace std;
    
    class Dog {
    private:
        int age;
        int weight;
    public:
        Dog();      //Constructor
        ~Dog();    //Destructor
        void setAge(int age);
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    };
    
    Dog::Dog()
    {
        age = 0;
        weight = 0;
        cout << "Dog Constructor Called" << endl;
    }
    
    Dog::~Dog()
    {
        cout << "Dog Destructor Called" << endl;
    }
    
    void Dog::setAge(int age)
    {
        this->age = age;
    }
    
    int Dog::getAge()
    {
        return age;
    }
    
    void Dog::setWeight(int weight)
    {
        this->weight = weight;
    }
    
    int Dog::getWeight()
    {
        return weight;
    }
    
    void Dog::speak()
    {
        cout << "BARK!!" << endl;
    }
    
    int main()
    {
        {
            Dog fido;
            Dog rover;
    
            cout << "Rover is " << rover.getAge() << " years old." << endl;
            cout << "He weighs " << rover.getWeight() << " lbs." << endl;
            cout << endl;
    
            cout << "Updating Rover's Age and Weight" << endl;
            rover.setAge(1);
            rover.setWeight(10);
    
            cout << "Rover is " << rover.getAge() << " years old." << endl;
            cout << "He weighs " << rover.getWeight() << " lbs." << endl;
            cout << endl;
    
            cout << "Fido is " << fido.getAge() << " years old." << endl;
            cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    
            cout << "Setting Fido to be the same as Rover" << endl;
            fido = rover;
    
            cout << "Fido is " << fido.getAge() << " years old." << endl;
            cout << "He weighs " << fido.getWeight() << " lbs." << endl;
    
            rover.speak();
            fido.speak();
        }
    
        cout << "Destructors should have already been called!" << endl;
    
        return 0;
    }

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Do you have the latest version of Dev-C++? Dev-C++ uses gcc for its compiler, which I've always considered one of the best.

  6. #21
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by bithub
    That's true jlou, but my original point still stands. It is best to over-ride equals to ensure everything in your class gets copied over correctly.
    Yeah, although I would only recommend overriding it if the default ones don't do what you want.


    Quote Originally Posted by stickman
    Added this code and it works

    fido.~Dog();
    rover.~Dog();
    Bad idea - don't call the destructors explicitly unless you are using special memory management and you know what you are doing. In this case, the destructors will be called twice, which is bad.

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Jlou makes a good point. The destructors may get called after the console window disappears.

  8. #23
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by swoopy
    The destructors may get called after the console window disappears.
    How can that be? As soon as it close, the program stops, so wouldn't it be impossible for it to destruct after it closes?

  9. #24
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by stickman
    How can that be? As soon as it close, the program stops, so wouldn't it be impossible for it to destruct after it closes?
    More like just before the window closes, and then the window closes and you don't see it.

  10. #25
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Quote Originally Posted by jlou
    More like just before the window closes, and then the window closes and you don't see it.
    Oh, ok.

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >More like just before the window closes, and then the window closes and you don't see it.
    Well, if that's the case, then Stickman, can you open up a console from the Start menu, and just run the exe that way? That would tell us for sure.

    The destrctor messages should show up if you do this.

  12. #27
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    The destructor message didn't show up.

  13. #28
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    And what about in my modified version with the extra scope?

  14. #29
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Yeah, it showed up.

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Well I'm baffled. But Stickman, you also ran Jlou's program above with the extra scope, and they do show up?

    EDIT: question answered

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Problem: Stupid Compiler Error
    By iSlak in forum C++ Programming
    Replies: 4
    Last Post: 08-22-2005, 06:34 AM
  2. Stupid compiler
    By chris1985 in forum C Programming
    Replies: 5
    Last Post: 07-01-2005, 08:22 AM
  3. Dev C++ Compiler, Indentation?
    By Zeusbwr in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 07:13 AM
  4. lcc win32 compiler download problems
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-01-2004, 08:39 PM
  5. Special Compiler for win app's
    By Unregistered in forum Windows Programming
    Replies: 19
    Last Post: 04-26-2002, 04:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21