destructor(): in and out of class differences?

This is a discussion on destructor(): in and out of class differences? within the C++ Programming forums, part of the General Programming Boards category; is there a difference between having a destructor in or out of the class? which one is better? having the ...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    destructor(): in and out of class differences?

    is there a difference between having a destructor in or out of the class? which one is better? having the destructor inside the class seems more readable. but my book seems to have the destructor accessed within the scope of the class.
    Code:
    ////////////////////////////////////////////
    class linklist	//a list of links
    {
    private:
    	link* first;		//pointer to the first link
    public:
    	linklist()			//no-arg constructor
    	{
    		first = NULL;	//no first link
    	}
    
    	~linklist()			//destructor
    	{
    		link* current = first;	//set ptr to first link
    		while( current != NULL )		//quit on last link
    		{
    			link* temp = current;
    			current = current->next;
    			delete temp;
    			cout << "object destroyed." << endl;
    		}
    	}
    
    	void additem(int d);	//add data item (one link)
    	void display();			//display all links
    };
    //------------------------------------------
    void linklist::additem(int d)		//add item
    {
    	link* newlink = new link;		//make a new link
    	newlink->data = d;				//give it data
    	newlink->next = first;			//it points to the next link
    	first = newlink;				//now first points to this
    }
    //------------------------------------------
    //linklist::~linklist()
    //{
    //	link* current = first;	//set ptr to first link
    //	while( current != NULL )		//quit on last link
    //	{
    //		link* temp = current;
    //		current = current->next;
    //		delete temp;
    //		cout << "object destroyed" << endl;
    //	}
    //}

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The difference is that when it's defined inside the class definition, it's called, "inline". This is usually done with very small functions that are called often, because it can reduce the amount of overhead needed to run that function, and can make your program more efficient. For more information, just look up inline functions in your book.

  3. #3
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    so basically if the function is only going to be run once per object define it outside of the class?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If the destructor is short like
    Code:
    ~Person() {}
    or maybe just a few lines of code in the body of the function, then go ahead and make it inline by defining it in the class declaration. If the destructor, or other function/method has more than a few lines, define it outside the classs. What constitutes a "few"?
    Who knows. But anything more than a simple loop to release memory and I'd define it outside the declaration. Een if you put it in the declaration the compiler doesn't have to inline it.
    You're only born perfect.

  5. #5
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    thanks for the tips. ill keep that in mind.

Popular pages Recent additions subscribe to a feed

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