I'm currently implementing a copy constructor for a Linked List class. When I create a new instance of the class with another Linked List as a parameter, the constructor is being called for the object I'm passing as the parameter. This is leaving me confused beyond belief. Here is the section necessary to understand what's going on in the main method:
int main()
{
LinkedList ll;
LinkedList ll2(ll);
}
So, instead of calling the copy constructor for ll2, the copy constructor for ll is being called. I've confirmed that the size of ll is correctly 3 before I attempt to copy ll into a new LinkedList, namely ll2. After the copy though, both have the same size, greater than 3, but even more weird, the copy constructor of ll is being called, not that of ll2. Since I'm using VC++, I've stepped through the program to confirm this.
Here is the copy constructor for the LinkedList class:
LinkedList::LinkedList(const LinkedList & other)
{
LLNode *otherCurNode = other.GetFirst();
if (otherCurNode != NULL)
{
front = new LLNode(otherCurNode->GetValue(), NULL, NULL);
back = front;
}
else
{
front = NULL;
back = NULL;
}
LLNode *curNode = front;
while (otherCurNode != NULL)
{
Insert(otherCurNode->GetValue(), curNode);
curNode = curNode->GetNext();
otherCurNode = otherCurNode->GetNext();
back = curNode;
}
numNodes = other.GetSize();
}
My apologies if this ends up being a simple problem - I'm fairly new to C++. Any help will be greatly appreciated!