Linked list with and without destructor

I have a linked list like this:
1
2
3
4
5
class list{
    list *prevList;
    list *nextList;
    component *A[10];
}


Now I want to have another linked list exactly like this but in this I want to also add a destructor.

So I implemented it by deriving it from the initial list class
1
2
3
4
5
6
7
8
9
10
class newList : public list{
          // Destructor
    ~list();

}
newList::~newList(){
    for(i=0;i<10;i++)
       if(A[i])
          delete A[i];
}


Now the problem is when I create a linked list out of newList (with 1st pointer as B) and want to delete it I have a function like this:
1
2
3
4
5
6
7
	newList *nl;
	while(B)
	{
		nl = B->nextList;
		delete ;
		B = nl;
	}


The problem here is that I get a compiler error saying that it cannot make the assignment nl = B->nextList; since nextList is a list object and nl is a newList object.

So how do I go about deleting this linked list?


Last edited on
Well, in this case you could just cast one to another ( http://www.cplusplus.com/doc/tutorial/typecasting/ )
But generally it is a bad idea to derive from a class without a virtual destructor.
I would probably declare 'B' and 'nl' to be of type 'list *' rather tan of type 'newList *', but this idea requires that you also add a 'no-op' virtual destructor to the 'list' class. If you don't, your ~newList function will never get called.
Topic archived. No new replies allowed.