I have the following class:
class Stack {
struct Link {
void* data;
Link* next;
void initialize(void* dat, Link* nxt);
}* head;
public:
void initialize();
void push(void* dat);
void* peek();
void* pop();
void cleanup();
};
The pop
method is:
void* Stack::pop() {
if(head == 0) return 0;
void* result = head->data;
Link* oldHead = head;
head = head->next;
delete oldHead;
return result;
}
oldHead
is a pointer to a struct Link
, which has a void pointer as member. So by deleting oldHead
I'm implicitly deleting that void pointer, right?
I'm reading Thinking in C++ by Bruce Eckel, and it says that deleting void pointers doesn't clean things up properly because delete
needs to know the type of the pointer.
This code is implicitly deleting the void pointer data
, so: Can someone explain why is this (implicit) way of deleting a void pointer different from deleting with delete <void pointer>
?