Thread: n00b question... deleting an object

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    n00b question... deleting an object

    how do you delete an object once you create it?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    With what's called a destructor. Your compiler automatically supplies a default destructor if you don't define one, and a destructor is automatically called when your object goes out of scope, like when your program ends.

    Try this code:
    Code:
    include <iostream>
    using namespace std;
    
    class MyClass
    {
    public:
        MyClass() //Constructor
        {
    	cout<<"Constructor called."<<endl;
        }
    
        ~MyClass() //Destructor
        {
    	cout<<"Destructor called."<<endl;
        }
    
        int number;
    };
    
    
    int main()
    {
    	MyClass an_array[5];
    	
    	cout<<"In main()."<<endl;
    
    	return 0;
    	
    }
    Last edited by 7stud; 05-05-2003 at 05:38 PM.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    how do u actually delete the object?

    is it anything like
    delete <object name>;
    free <object name>;
    ???

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    I was editing my post to address that question specifically. Try re-reading it to see if it explains it now.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    thanks for the quick responce, but i was wondering if there was a manual way of deleting it. its not THAT big of a deal, but i have a class just for creating a gui on screen, so once thats one, i just wana have it deleted instead of it hanging around in memory

    i COULD call it in another function, but i dont wanna have to write another function specifically just for one object.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
    MyClass *myclass = new MyClass;
    MyClass *myclassarr = new MyClass[10];
    
    
    delete myclass;
    delete[] myclassarr;
    Use the delete keyword to free an object. You must pass a pointer to it just as with free(). When deleting an array, use delete[].

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array class object question
    By ExDHaos in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2009, 11:23 AM
  2. Another n00b question: storing text as a variable
    By Jmcrofts in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2007, 08:12 AM
  3. n00b question regarding classes
    By hobbes67 in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 02:47 PM
  4. n00b question.... dat file related
    By hobbes67 in forum C Programming
    Replies: 7
    Last Post: 06-24-2005, 11:39 AM
  5. Deleting question
    By face_master in forum C++ Programming
    Replies: 30
    Last Post: 09-20-2001, 02:30 AM