int main(){
//Node is some template class
Node<int>* head = new Node<int>[5];
for(int ii = 0; ii < 5; ii++)
{
head[ii].set_Data(ii);
head[ii].set_Link(head + (ii + 1));
if(ii == 4)
{
head[ii].set_Link(NULL);
}
}
delete [] head;
}
template<typename T>
void Node<T>::set_Link(Node* Node_Address)
{
Link = Node_Address;
}
template<typename T>
Node<T>::~Node()
{
delete Link;
cout << "Destructor" << endl;
}
I'm learning linked lists right now. I don't get why my destructor is called 15 times and cout statement is printed 15 times. If I get rid of the statement
head[ii].set_Link(head + (ii + 1));
The destructor is only called 5 times, which makes sense since 5 classes are made. Why is the destructor called when I use the member function set_Link(), when I am only passing a pointer, not a class. The copy constructor isn't even called. Thanks for any help!