SiggyxLeGiiT
SiggyxLeGiiT

Reputation: 33

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
}

Upvotes: 1

Views: 266

Answers (1)

ravi
ravi

Reputation: 10733

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...

Upvotes: 1

Related Questions