Thread: plz help with deleting class deconstructor.

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    plz help with deleting class deconstructor.

    **just realized my topic title makes no sense.. apologize ahead of time.

    I'm having a hard time with this code.. plz help me out..


    Code:
    #include<iostream>
    using namespace std;
    
    char input[101];
    
    class NameAdd
    {
    	char* name;
    	char* address;
    
    	public:
    		NameAdd()	{
    				this->name = "my_name";
    				this->address = "my_address";
    		}
    		~NameAdd()	
    		{	
    			cout << "Deleting the base!!!" << endl;
    			delete this->name; delete this->address;
    		}
    };
    
    class amountnum : virtual public NameAdd
    {
    	char* amount;
    
    	public:
    		amountnum()	
    		{		
    				this->amount = "a_amount";
    		}
    		~amountnum()	
    		{
    			cout << "Deleting the osap!!!" << endl;
    			delete this->amount;
    		}
    };
    
    
    int main()
    {
    	NameAdd * formlist[5];
    	int limit = 1;
    	formlist[limit] = new amountnum();
    	system("pause");
    	delete formlist[limit];
    	cout << "success!" <<endl;
    	system("pause");
            return 0;
    }
    i know the delete isn't working. i was hoping someone could shine some light as to whats wrong.
    Last edited by MegaManZZ; 08-31-2008 at 09:16 AM.

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    #include<iostream>
    using namespace std;
    
    char input[101];
    
    class NameAdd
    {
    	char* name;
    	char* address;
    
    	public:
    		NameAdd()	{
    				this->name = "my_name";
    				this->address = "my_address";
    		}
    		virtual ~NameAdd()	// Destructor should be virtual in this case
    		{	
    			cout << "Deleting the base!!!" << endl;
    			//delete this->name; delete this->address;   // DON'T. The value name and adress hold weren't allocated with new
    		}
    };
    
    class amountnum : virtual public NameAdd
    {
    	char* amount;
    
    	public:
    		amountnum()	
    		{		
    				this->amount = "a_amount";
    		}
    		~amountnum()	
    		{
    			cout << "Deleting the osap!!!" << endl;
    			//delete this->amount;   // Same thing here
    		}
    };
    
    
    int main()
    {
    	NameAdd * formlist[5];
    	int limit = 1;
    	formlist[limit] = new amountnum();
    	system("pause");
    	delete formlist[limit];
    	cout << "success!" <<endl;
    	system("pause");
            return 0;
    }
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    god bless these forums..
    thank you foxman.
    Last edited by MegaManZZ; 08-31-2008 at 09:44 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You only need to delete what you allocate with new
    Btw, you don't need to prefix "this->" when accessing members either. I don't know if you know or not...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Also, unless there's more code that you haven't shown, you probably don't need virtual inheritance when deriving class amountnum. The virtual destructor is enough.
    Last edited by rudyman; 08-31-2008 at 10:30 AM.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, if you would have allocated those strings with new, then you were using the wrong version of delete. To delete dynamic arrays you need to use
    Code:
    delete [] ptr;
    not just
    Code:
    delete ptr;

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also, never assign string literals to non-const char pointers.

    Just use std::string.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM