C++ 复制具有两个链表的对象的构造函数C++;
我试图为一个有两个链表的对象创建一个复制构造函数。我想让我的副本知道对原始对象的引用。我写了一个副本构造函数,它制作了一个与原件完全相同的副本,但它仍然引用了原件,我找不到它在哪里做。这是我的密码C++ 复制具有两个链表的对象的构造函数C++;,c++,C++,我试图为一个有两个链表的对象创建一个复制构造函数。我想让我的副本知道对原始对象的引用。我写了一个副本构造函数,它制作了一个与原件完全相同的副本,但它仍然引用了原件,我找不到它在哪里做。这是我的密码 InfiniteBools::InfiniteBools(InfiniteBools* original) { if (original == NULL) return; //sets the head of copy to new linked list nodes that ar
InfiniteBools::InfiniteBools(InfiniteBools* original) {
if (original == NULL) return;
//sets the head of copy to new linked list nodes that are copies of original
InfiniteBools* copy = new InfiniteBools();
copy->nonNegHeader = new LinkedListNode(original->nonNegHeader->value,NULL);
copy->negHeader = new LinkedListNode(original->negHeader->value, NULL);
LinkedListNode* tempPos = original->nonNegHeader;
LinkedListNode* tempNeg = original->negHeader;
LinkedListNode* copyTempPos = copy->nonNegHeader;
LinkedListNode* copyTempNeg = copy->negHeader;
tempPos = tempPos->next;
while (tempPos != NULL) {
// Allocate the next node and set current node next to the new node.
copyTempPos->next = new LinkedListNode(tempPos->value,NULL);
copyTempPos = copyTempNeg->next; //move temp to the new node
tempPos = tempPos->next; //move temp of original foward
}
tempNeg = tempNeg->next;
while (tempPos != NULL) {
copyTempNeg->next = new LinkedListNode(tempNeg->value, NULL);
copyTempNeg = copyTempNeg->next;
tempNeg = tempNeg->next;
}
}
我已经试过了,但我找不到哪里出了问题。如果你知道我做错了什么,请告诉我
多谢各位
编辑:
InfiniteBools是一个对象,包含对两个节点的引用,这两个节点是两个链表的头。
每个节点或LinkedListNode都包含一个布尔值和指向下一个节点的指针。首先:复制构造函数通常接受常量引用:
InfiniteBools::InfiniteBools(InfiniteBools const& original);
那么您已经在构建一个新对象了。为什么是第二个内部对象<代码>此是您的副本:
//InfiniteBools* copy = new InfiniteBools();
// would have resulted in a memory leak!!!
this->nonNegHeader = new LinkedListNode(original->nonNegHeader->value, nullptr);
this->negHeader = new LinkedListNode(original->negHeader->value, nullptr);
// prefer C++ keyword (nullptr) over (obsolete) C-style macro (NULL)
您可以省略这个->
,只要没有其他变量与您的成员同名(前者然后隐藏后者)
您忘了检查是否有空列表的原件
代码的其余部分乍一看是正确的(不过,请注意,如果您有一个常量引用,则需要常量指针来迭代其列表节点!),但没有显式测试。如果不复制交错,则只需要一组变量。如果在节点周围编写一个单独的类LinkedList
,并在其自己的复制构造函数中进行复制,则可以避免代码重复
实际上,已经有一个完全实现的链表:。它是双链接的,如果你想要一个单链接的列表,还有。您应该更喜欢现成的实现,而不是重新发明轮子(除非您这样做是为了练习…。您所展示的不是正确的复制构造函数。首先,它不通过const引用获取其输入对象,这是一个要求。另一方面,它不设置要复制到的对象的
nonNegHeader
和negHeader
成员。它构造第三个要复制到的对象,然后将其泄漏
请尝试类似以下内容:
InfiniteBools::InfiniteBools(const InfiniteBools& original)
: nonNegHeader(NULL), negHeader(NULL)
{
//sets the head of this to new linked list nodes that are copies of original
LinkedListNode* tempOrig = original.nonNegHeader;
LinkedListNode** tempCopy = &nonNegHeader;
while (tempOrig) {
// Allocate the next node and set current node next to the new node.
*tempCopy = new LinkedListNode(tempOrig->value, NULL);
tempCopy = &(tempCopy->next); //move temp to the new node
tempOrig = tempOrig->next; //move temp of original forward
}
tempOrig = original.negHeader;
tempCopy = &negHeader;
while (tempOrig) {
// Allocate the next node and set current node next to the new node.
*tempCopy = new LinkedListNode(tempOrig->value, NULL);
tempCopy = &(tempCopy->next); //move temp to the new node
tempOrig = tempOrig->next; //move temp of original forward
}
}
那当然不是复制品。复制构造函数将实例引用而不是地址作为参数。我们不知道
InfiniteBools
,也不知道LinkedListNode
是如何定义的。考虑一下:你有一个充满代码的项目。我们只有你在这里发布的内容,没有更多。请提供一个链接列表。只是想知道:为什么你需要一个链接列表呢?无论您做什么,基于数组的实现可能会更高效,最好使用标准容器:std::vector
,或者,如果您想避免bool(将多个位打包在一起)的专门化,std::vector
。感谢您的帮助。如果我传入const&original,我仍然需要一个指向对象的指针,以便执行以下操作:original->nonNegHeader->value您需要指向列表节点的指针(对于original
,这些节点需要是const,而不是original
本身(但是您可以通过操作符访问原始
。
而不是操作符->
:原始.nonNegHeader->value
)。