destructor class function

Jul 19, 2011 at 12:58pm
I need to build destructor class function for this but i dont know how.
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <string>
using namespace std;

class node {
public:
	string name;
	node *next;
	node(string input) {name = input; next = NULL;};
    
};


for string class it was
1
2
3
4
~Stirng();
Stirng::String(){
delete [] ptr;
}


but for this i dont know
Jul 19, 2011 at 3:32pm
Firstly, the destructor you posted doesn't have ~, though that might have been a typo.

Do you need to delete next ? I don't see where the problem is..
Jul 19, 2011 at 5:17pm
Revise the example so that it destroys nodes upon exit. this is what i must do.
Jul 19, 2011 at 9:43pm
Well, then write a destructor and put delete next; in it. If you don't know how to write one, see http://www.cplusplus.com/doc/tutorial/classes/ (around middle)
Last edited on Jul 19, 2011 at 9:43pm
Jul 19, 2011 at 10:15pm
so basiclly. ~node() {delete next;}
Jul 20, 2011 at 10:19am
or what?
Jul 20, 2011 at 10:50am
Pretty much yes, just be sure you don't delete memory thats not yours (NULL/Uninitialized)
Jul 20, 2011 at 11:02am
well then giveme a code that only deltes the memory used by this program.
Jul 20, 2011 at 11:06am
Wouldn't you gain more from it if you figured it out on your own?
Jul 20, 2011 at 12:10pm
true, but my comp is already pretty fucked up, so i dont wanan fuck it up even more.
Jul 20, 2011 at 1:18pm
'comp' is short for what?
Your code is fine. Whether you have any problems with it depends on how you use it.
Jul 20, 2011 at 1:21pm
comp as i computer
Jul 20, 2011 at 1:52pm
You can't fuck up your computer this way.
Jul 20, 2011 at 3:55pm
Do your own homework
Jul 20, 2011 at 4:46pm
not a homework. self study. school doesent teach this.
Topic archived. No new replies allowed.