Thread: Using Destructors

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    31

    Question Using Destructors

    I am having difficulty understanding the purpose of using a destructor function.

    If I don't have a destructor function, objects will still be destroyed at the end of a function, and when the program terminates.

    For what reason would a destructor function be necessary?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Mostly to delete any memory that your object allocated using the new keyword; but sometimes other things need to be closed properly as well.
    Ex.

    This has a memory leak:
    Code:
    class Bad
    {
    public:
        Bad() : m_Str( new char[ 50 ] ) {}
    
    private:
        char*  m_Str;
    };
    This code doesn't have a memory leak:
    Code:
    class Good
    {
    public:
        Good() : m_Str( new char[ 50 ] ) {}
    
        ~Good() { delete [] m_Str; }
    
    private:
        char*  m_Str;
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    There are two special member function during an object life-time, constructor and destructor. A object that is destroyed by using delete and destroys automatically while exiting a scope, the destructor would be invoked. This is necessary to guarantee the resouces to be released correctly.

    >>If I don't have a destructor function, objects will still be destroyed at the end of a function, and when the program terminates.

    Yes, but it still has a destructor. The default destructor created by compilier may be trivial, or calls the destructors of the base class and members of the derived class.

    Code:
    class A
    {
    public:
    	A()
    	{
    		i = new int;
    	}
    
    /* if you do not give a destructor like this, the memory leak that refered by i, because the destructor inserted by compiler is trival, nothing to do.
    	~A()
    	{
    		delete i;
    	}
    */
    private:
    	int *i;
    };
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by RaisinToe View Post
    I am having difficulty understanding the purpose of using a destructor function.

    If I don't have a destructor function, objects will still be destroyed at the end of a function, and when the program terminates.
    Because of the default constructor that is generated for you if you don't write one. If your class allocates objects or resources that don't clean themselves up, you need to write a destructor to do that. For example, if you have a std::string member variable, its destructor will properly clean it up when yours (default or written by you) runs. If you have a pointer to a std::string as a member, however, you must delete that pointer in your destructor.

    Google RAII.

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, 03:07 PM
  4. Destructors in dynamically allocated arrays
    By frenchfry164 in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2003, 11:26 PM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM