gpt4 book ai didi

c++ - 我的代码是在创建对象的深拷贝还是浅拷贝?

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:14 24 4
gpt4 key购买 nike

我有这段代码,它接受一个由外部指针和带有链表的内部指针组成的堆栈,但我无法判断它是在进行深拷贝还是浅拷贝。如果我没有足够的信息,我很抱歉,我认为这是你需要告诉的。谢谢!

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
}

最佳答案

在这种情况下,它只是复制指针,即浅拷贝有效。

深拷贝意味着不仅仅是复制指针,您还为新指针显式分配内存并将传入指针的内容存储到您的代码恰好丢失的内容中...

关于c++ - 我的代码是在创建对象的深拷贝还是浅拷贝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26901835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com