SiggyxLeGiiT SiggyxLeGiiT -4 years ago 101
C++ Question

Is my code creating a deep or shallow copy of my object?

I have this code which takes a stack made up of a external pointer and internal pointers with linked lists, I can't tell if it's making a deep or shallow copy though. If I don't have enough information I'm sorry, I think this is what you need to tell though. Thank you!

void StackClass::operator =(const StackClass& orig)
{
//stack = nullptr;
node* temp = orig.stack;
if (!orig.IsEmpty())
{
while (temp != NULL)
{
stack = orig.stack; // sets thew new stack equal to the old stack's value
temp = temp->next;
} // end while loop
} // end if
else
{
stack = nullptr; // sets it to an empty list because there's no values
} // end else
}

Answer Source

In this case it's just copying the pointer i.e shallow copy is in effect.

Deep copy means rather than just copying pointer you explicitly allocate memory for your new pointer and store the contents of passed in pointer into that which your code happens to be missing...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download