Thread: How to use a destructor

  1. #1
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167

    Question How to use a destructor

    The below code creates a "Box" class; and then an object "thisbox"; how can I uncreate the object?

    Code:
    	#include <iostream>
    
    	class Box {
    	private:
    		int height, width, depth;
    	public: 
    		Box(int, int, int);
    		~Box();
    		int volume();
    	};
    	
    	Box::Box(int ht, int wd, int dp)
    	{
    		height = ht;
    		width = wd;
    		depth = dp;
    	}
    	
    	Box::~Box()
    	{
    	}
    	
    	int Box::volume()
    	{
    		return height * width * depth;
    	}
    	
    	int main()
    	{
    		Box thisbox(7, 8, 9);
    		int volume = thisbox.volume();
    		std::cout << volume; 
    		return 0;
    	}
    Last edited by Noobie; 12-02-2003 at 11:09 PM.
    AIM: MarderIII

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    To my understanding, you implement a destructor to free up/deallocate resources that is used by the object (e.g. memory, files). The object itself is destroyed, but not by the destructor, when it goes out of scope. Looking at your constructor, I don't see the need to implement a destructor. Note that when you don't have one, the compiler will create an empty destructor.

  3. #3
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    I don't need one; but I want to learn how to do it.
    AIM: MarderIII

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Code:
    Box::Box(int ht, int wd, int dp)
    {
      height = ht;
      width = wd;
      depth = dp;
    }
    When an object of Box goes out of scope, "height", "width", and "depth" get destroyed also, so, in the case of your class Box, there's no "housekeeping" needs to be done by the destructor (unless I miss something). Whereas, say, you modify the constructor to be like this.
    Code:
    Box::Box(int ht, int wd, int dp)
    {
       height = ht;
       width = wd;
       depth = dp;
       items = new int[5];    // say you want to store numbers in the box
    }
    
    Box::~Box()
    {
       delete [] items;   // deallocate space for 5 integers in the memory
    }
    If you don't have delete [] items in your constructor, when the object is destroyed, items (the pointer to the array of 5 ints)will be gone, but the int[5] is still there occupying the memory (and you get yourself memory leak)
    Last edited by alphaoide; 12-02-2003 at 11:55 PM.

  5. #5
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    what is meant by "goes out of scope"?

    is there a way to delete the height and width? I want to delete the entire instance of the class.

    I think this is what I want:
    Code:
     #include <iostream.h> 
     class Dog {
     public:
             Dog() { std::cout << "bow"; }
             ~Dog() { std::cout << "wow"; }
     };
     int main() {
             Dog *rex = new Dog();
             delete rex;
    		 rex = new Dog();
    		 delete rex;
    
    	return 0;
     }
    Last edited by Noobie; 12-03-2003 at 12:21 AM.
    AIM: MarderIII

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    There are four scopes of identifier; what you need to know are function and block scope. In your code you declare the object inside the function main(); therefore, "goes out of scope" means exit the main().
    For block scope, study the following.
    Code:
    int main()
    {
       int x = 1;
       cout << "x = " << x << endl;       // output x = 1
       
      { // beginning of the block
          int x = 3;             
          cout << "x = " << x << endl;   // output x =3
       }  // end of the block, outside the scope of x = 3
    
       cout << "x = " << x << endl;     // output x =1
       
       return 0;
    }

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Like I said, height weight and depth are gone when the object goes out of scope. But if you want to destroy it before it goes out of scope then...
    Keep class Box that you have
    And like you did with Dog, do
    Code:
    Box *thisbox = new Box(0, 0, 0);
    delete thisbox;
    And everything about the object Box pointed by thisbox is gone. thisbox (the pointer) still exists until it goes out of scope

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2009, 12:05 AM
  2. exception in the destructor
    By coletek in forum C++ Programming
    Replies: 3
    Last Post: 01-12-2009, 12:01 PM
  3. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  4. Destructor inaccessible
    By renanmzmendes in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2008, 11:07 AM
  5. destructor question
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2006, 11:29 AM