Destructor of linked list

Hey all,

So I have a pretty basic question. I need to call the destructor of a linked list that I created. I don't know why I cannot figure out the syntax to do so. Here is the needed code below. I'm not sure why I can't grasp the concept of how to call it just by looking at it, as I had no problems writing the destructor itself. Maybe just don't entirely understand how it's used...

Basically I have a Vlist object in main.cpp that is named list. How would I
call the destructor on this object?

Vlist.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 class Vlist{
	public:
		Vlist();
		~Vlist();
		bool insert(Video *type);
		bool lookup(string search);
		void print();
		void print_by_rating();
		void print_by_length();
		bool remove(string search);
		int length();

	private:
		class Node{
			public:
				Video *m_video;
				Node *m_next;
				Node(Video *type, Node *next){
					m_video = type;
					m_next = next;
				}
		};
		Node *m_head;


Vlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Vlist::~Vlist()
{
 

    Node *ptr = m_head;
    while (ptr != NULL)
    {
        Node *temp;
    
        temp = ptr;
        ptr = ptr->m_next;
        delete temp;
    }
}
use the delete operator.

1
2
3
4
5
6
int main() {
    Vlist *mylist = new Vlist();
    cout << "Calling destructor!\n";
    delete mylist;
    cout << "Destructor called!\n";
}
Last edited on
@KvltKitty,
You cannot call a destructor by yourself. It is called automatically when an object of the class goes out of the scope.
Topic archived. No new replies allowed.