【发布时间】:2017-11-24 15:21:00
【问题描述】:
我一直在构建一个模板链接列表,最近实现了一个用户定义的复制构造函数来处理我的链接列表的深层副本。我当前的解决方案编译得很好,但是当我执行时,什么都没有显示。
#include <iostream>
template<typename T>
class LinkedList
{
private:
struct Node
{
T data;
Node* next;
};
Node* head;
Node* tail;
int size;
public:
LinkedList();
LinkedList(const LinkedList<T> &list);
~LinkedList();
LinkedList<T>& operator=(const LinkedList<T> list);
int getSize() const;
void display() const;
void push(const T &data);
void insert(const int pos, const T &data);
void remove(const int pos);
};
template<typename T>
LinkedList<T>::LinkedList() : head{ nullptr }, tail{ nullptr }, size{ 0 }
{
}
// TODO: User-defined copy constructor
template<typename T>
LinkedList<T>::LinkedList(const LinkedList<T> &list)
: head{ nullptr }, tail{ nullptr }, size{list.size}
{
std::cout << "In the copy constructor\n";
if (list.head == nullptr)
{
return;
}
else
{
Node* curNode = new Node{list.head->data, nullptr};
head = curNode; // sets the head member variable to first node in Linked List
Node* curListNode = list.head;
while (curListNode->next != nullptr)
{
curNode->next = new Node{curListNode->next->data, nullptr};
curListNode = curListNode->next;
curNode = curNode->next;
}
curNode->next = new Node{curListNode->next->data, nullptr};
tail = curNode->next;
}
}
template<typename T>
LinkedList<T>::~LinkedList()
{
Node* prevNode = head;
Node* curNode = head;
while(curNode != nullptr)
{
prevNode = curNode;
curNode = curNode->next;
delete prevNode;
}
head = nullptr;
tail = nullptr;
}
template<typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T> list)
{
// make a copy of each node - much like the copy constructor
std::cout << "In the overloaded assignment\n";
}
template<typename T>
int LinkedList<T>::getSize() const
{
return size;
}
template<typename T>
void LinkedList<T>::display() const
{
Node* curNode = head;
while (curNode != nullptr)
{
std::cout << curNode->data << '\n';
curNode = curNode->next;
}
std::cout << '\n';
}
template<typename T>
void LinkedList<T>::push(const T &data)
{
Node* newNode = new Node{data, nullptr};
if (size == 0)
{
head = newNode;
tail = newNode;
}
else
{
tail->next = newNode;
tail = newNode;
}
++size;
}
template<typename T>
void LinkedList<T>::insert(const int pos, const T &data)
{
if (pos < 0 || pos > size)
{
throw "Index is out of range!";
}
else if (pos == size)
{
push(data);
}
else
{
Node* newNode = new Node{data, nullptr};
Node* prevNode = head;
Node* curNode = head;
int i = 0;
while (i != pos)
{
prevNode = curNode;
curNode = curNode->next;
++i;
}
prevNode->next = newNode;
newNode->next = curNode;
++size;
}
}
template<typename T>
void LinkedList<T>::remove(const int pos)
{
if (pos < 0 || pos > size)
{
throw "Index is out of range!";
}
else if (size == 0)
{
throw "List is empty!";
}
else
{
Node* prevNode = head;
Node* curNode = head;
int i = 1;
while (i != pos)
{
prevNode = curNode;
curNode = curNode->next;
++i;
}
prevNode->next = curNode->next;
delete curNode;
if (pos == size)
{
tail = prevNode;
}
--size;
}
}
int main()
{
LinkedList<int> list;
list.push(1);
list.push(3);
list.push(6);
list.display();
LinkedList<int> copyList{list};
copyList.display();
return 0;
}
当我将copyList 语句添加到main() 时,甚至不显示第一个LinkedList<int> list。但是,当我注释掉这两个 copyList 语句时,list 会正常显示。
我添加了打印语句以查看是否正在调用复制构造函数或operator= 函数,并且当copyList 语句添加到我的程序中时,它们都没有打印到控制台(代码编译得很好) .
我可以使用任何帮助来帮助诊断导致此行为的原因。
【问题讨论】:
-
首先,您不应该依赖复制构造函数中的副作用,因为复制省略是一回事。但是,不应省略复制分配。 /OT:
I've been building on a template Linked List我很欣赏您很可能将其作为练习,无论是为了上课还是您自己的研究,但理想情况下,人们永远不需要在 C++ 中实现这样的东西,因为它有一个丰富的标准库,应该提供开箱即用的常见内容,在本例中为std::list(除非您有非常具体的要求并确定这是一个瓶颈。 -
您能否详细说明副作用的含义?完全同意您对使用
std::list的评论,我只是将其作为学术练习。 -
副作用:复制构造函数做任何可以观察到的事情。见stackoverflow.com/questions/28659879/…
标签: c++ linked-list copy-constructor deep-copy