destructors

This is a discussion on destructors within the C++ Programming forums, part of the General Programming Boards category; howdy, if a destructor is put into a class and its function body has no delete keyword in it. Code: ...

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

    destructors

    howdy,
    if a destructor is put into a class and its function body has no delete keyword in it.

    Code:
    stuff::~stuff(){
    cout<<"am i Destructive!"<<endl;
    }
    will it over ride the compilers call to destruct the object or will it still act like a destructor and free up the resources used by the object?

    M.R.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If you define a destructor then the default destructor won't be used. It still acts like a destructor and reclaims memory allocated for the object, but not by the object. If you allocate memory with new in your class anywhere and don't free it, then you must be sure to check and see if it was freed and if not free it in the destructor. The default destructor won't do that for you anyway.

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

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

    a ittle more help

    howdy Prelude,
    i understand everything you said except this -
    but not by the object.
    do you mean if an object has been created its memory will not be freed up when it goes out of scope?

    M.R.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I mean if your class uses new to allocate memory for something then it has to explicitly delete that memory. The destructor doesn't do that for you by default.

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

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