Thread: A couple of OOP questions

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    A couple of OOP questions

    Hi,

    I've got a couple of OOP related questions that I wouldn't mind finding the answer to.

    1) Why is it that class destructors cannot be inherited? What if your derived class requires the same destructor code?

    2) If you create a class on the heap using the following:

    Code:
    Employee myEmployee = new Employee
    is the destructor called when you delete the object?

    Many thanks,

    Daniel
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Quote Originally Posted by codec
    1) Why is it that class destructors cannot be inherited? What if your derived class requires the same destructor code?
    Daniel
    The base class destructor should still be called. If you don't actually do any added work in the inherited class's destructor, you shouldn't have to worry about anything. If you do, make the base class have a virtual destructor. This will make sure the correct destructors are called when you use pointers and all that fun stuff.

    Quote Originally Posted by codec
    2) If you create a class on the heap using the following:

    Code:
    Employee myEmployee = new Employee
    is the destructor called when you delete the object?

    I could give you the answer, or you can go through the debugger step by step and find out for yourself

  3. #3
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks for the help.

    OK, maybe I should have been a little more detailed with question 2. I'm pretty sure it doesn't call the destructor, so should I be explicitly calling it instead? If not, what should I be doing? This is because my class also reserves memory on the heap, which the destructor frees, so it's quite important that it gets called.

    Many thanks,

    Daniel Briley
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Base class destructors are called when a derived class object is destroyed, so if the same code must be called in the derived class on destruction, it will happen in the base class destructor like skorman00 said. In fact, even if you don't want the base class destructor code to be called, it will be anyway. Of course, it is not really good design to have a class derive from another class but not want it to execute its destructor.

    For question two, it does call the destructor when you delete the pointer (your original example was missing the * to make myEmployee a pointer to Employee). Here is some sample code to help prove this to you:
    Code:
    #include <iostream>
    
    class TestBase
    {
        int data;
    public:
        TestBase(int i) : data(i) { }
        virtual ~TestBase() { std::cout << data << std::endl; }
    };
    
    class TestDerived : TestBase
    {
    public:
        TestDerived(int i) : TestBase(i) { }
        virtual ~TestDerived() { std::cout << "In Derived Destructor" << std::endl; }
    };
    
    int main()
    {
        TestDerived* t = new TestDerived(3);
        delete t;
    
        std::cin.get();
    }
    Notice how the destructors are called when you delete the object despite the fact that it was allocated on the heap. Also, note that the base class destructor is called automatically.

    Also note that a base class destructor should almost always be virtual, unless you specifically know why you don't want to.

  5. #5
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks again jlou, i really appreciate your help.
    C/C++ Support IRC Channel

    Server: irc.dal.net Channel: #csupport

    Come along and help make it a great resource for the community.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Adding on to jlou's comments, to see why you generally need the base class destructor to be virtual, run jlou's program with the following modifications:

    1) Make ~TestBase non-virtual.
    2) Make the following substitution:
    Code:
    // For
    TestDerived* t = new TestDerived(3);
    // Put in
    TestBase* t = new TestDerived(3);
    In this case, you'll notice that the base class destructor is always called, but the derived class destructor isn't. Making the destructor virtual ensures the proper destructor(s) are called.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  2. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 01:22 PM
  3. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 07:55 AM
  4. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 05:02 PM
  5. New to C++/ Couple of questions...
    By danielthomas3 in forum C++ Programming
    Replies: 13
    Last Post: 04-14-2002, 03:46 PM