destructors

This is a discussion on destructors within the C++ Programming forums, part of the General Programming Boards category; howdy, i'm a little confused about this destructor thing. im trying to get a handle on two concepts and they ...

  1. #1
    In The Light
    Join Date
    Oct 2001
    Posts
    598

    destructors

    howdy,
    i'm a little confused about this destructor thing. im trying to get a handle on two concepts and they seem to over lap, is
    ~cat()
    {
    }

    the same as

    delete cat();

    or in other words can a class be destructed using delete?

    and can an object created

    int * stuff = new int;

    be destructed

    ~stuff;

    it all seems so complicated.

    thanks
    M.R.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    A destructor is unique to classes and is used to free memory allocated by an object of a class.

    The delete operator is used to free memory that was dynamically allocated by new.

    An easy way to remember what needs a destructor and what needs delete is to remember that delete is an operator and must have a corresponding new to work, a destructor is a method of a class that cleans up the mess made by the constructor and the other methods of the class.

    They perform similar operations but in different ways and in different contexts. Note that a destructor shouldn't be explicitly called.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    A destructor is implicitly (you don't have to do it) called when the object goes out of scope. The purpose of the destructor is to deallocate any dynamic memory that was created by the object.
    here's a short example:
    Code:
    #include <iostream.h>
    
    class Object {
    private:
     int *array;
    
    public:
     // the constructor
     Object() { 
      // allocate a dynamic array
      array = new int[5]; 
      cout << "Object created\n";
     };
    
     // the destructor
     ~Object() { 
      // delete the array
      delete[] array;
      cout << "Object destroyed\n";
     };
    };
    
    void main() {
     Object Obj;
    }
    you never call the destructor, but the code runs when the object falls out of scope (in this case the program ends).

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    You can call the deconstructor but it's best not to. Deconstructors are automatically called when the object goes out of scope. Scope is simply anything between two { and } brackets. For example:
    Code:
    {
           int MyVariable = 1;
    }
    
    MyVariable += 2; // error. MyVariable is in another scope
    So, if you happen to want to destroy an object sooner than say, the end of a function, instead of calling the deconstructor explicitly you can call it implicitly with brackets.

    Code:
    voidMyFunc (int X, int Y)
    {
       {
            MyObject OBJA;
            OBJA.Z = (X*Y);
            // ... blahblah
       } // MyObject's destructor is called
       // ... blahblah
       return;
    }

  5. #5
    edwardtisdale
    Guest

    to add

    deletes are usually found in constructors while news in constructors. That way all the cleaning and regaining of space is done together.

  6. #6
    Unregistered
    Guest
    one more little thing.
    if you ever call delete on an object that has
    1.)not been created.
    2.)has allready been deleted.

    your program will become unstable
    the next time the deleted object is used it will likely crash or worse.

  7. #7
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    Thanks for your help!
    now let me see if i understand -
    like <rmullen3> said the destructor for an object is implicitly (but should never be explicitly) called when the object and/or class goes out of scope, i assume this does not apply to object created with the "new" operator based on what <taylorguitarman> said by calling "delete" in the class object destructor function. and if delete is misused it can cause major havoc.
    i think i'm getting the hang of this now.

    thanks
    M.R.

  8. #8
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    As mentioned above it is a good idea to check if a pointer is null before trying to delete it.
    so this simple code helps
    Code:
    if(NULL != pointer) {
     delete pointer;
     pointer = NULL;
    }
    you should also set the pointer to null after you have deleted it because technically it still points the memory location and you could accidently use it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a class have multiple destructors?
    By meili100 in forum C++ Programming
    Replies: 1
    Last Post: 05-14-2008, 05:28 PM
  2. Destructors in STL?
    By sawer in forum C++ Programming
    Replies: 4
    Last Post: 08-09-2006, 09:35 AM
  3. I always have memory errors in destructors
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2004, 02:07 PM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 10:26 PM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM

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