【问题标题】:Is this a deep copy and properly implemented = operator for a C++ Linked List?这是 C++ 链接列表的深层副本并正确实现 = 运算符吗?
【发布时间】:2011-08-08 22:17:14
【问题描述】:

现在我正在尝试使用我创建的双向链表类时遇到了一些错误。我对 = 运算符的实现如下所示:

template <typename T>
Dlist<T>& Dlist<T>::operator=(const Dlist &l)
{
    copyAll(l);
    return *this;
}

template <typename T>
void Dlist<T>::copyAll(const Dlist &l)
{
    node *copyList = new node;
    copyList = l.first;
    while(copyList){
        insertFront(copyList.first->o);
        copyList = copyList->next;
    }
    delete copyList;
}

请注意,o 是指向 List 中节点中数据的指针。

我的意图是让 copyAll 成为真正的深层副本。不是这样吗?我的类方法定义有问题吗?我是链表的新手,非常感谢您的帮助!

编辑:特别是我遇到的问题是当我制作一个列表并填写它,然后制作一个新列表并将其设置为等于第一个列表时,每当我对第二个列表执行某些操作时,它也会更改第一个列表.

EDIT2:这是课程本身。我不允许添加任何其他成员函数:

template <typename T>
class Dlist {
 public:

    // Operational methods

    bool isEmpty();
    // EFFECTS: returns true if list is empty, false otherwise

    void insertFront(T *o);
    // MODIFIES this
    // EFFECTS inserts o at the front of the list

    void insertBack(T *o);
    // MODIFIES this
    // EFFECTS inserts o at the back of the list

    T *removeFront();
    // MODIFIES this
    // EFFECTS removes and returns first object from non-empty list
    //         throws an instance of emptyList if empty

    T *removeBack();
    // MODIFIES this
    // EFFECTS removes and returns last object from non-empty list
    //         throws an instance of emptyList if empty

    // Maintenance methods
    Dlist();                                   // ctor
    Dlist(const Dlist &l);                     // copy ctor
    Dlist &operator=(const Dlist &l);          // assignment
    ~Dlist();                                  // dtor

 private:
    // A private type
    struct node {
    node   *next;
    node   *prev;
    T      *o;
    };

    node   *first; // The pointer to the 1st node (NULL if none)
    node   *last;  // The pointer to the 2nd node (NULL if none)


    void makeEmpty();
    // EFFECT: called by constructors/operator= to establish empty
    // list invariant

    void removeAll();
    // EFFECT: called by destructor/operator= to remove and destroy
    // all list elements

    void copyAll(const Dlist &l);
    // EFFECT: called by copy constructor/operator= to copy elements
    // from a source instance l to this instance
 };

【问题讨论】:

  • 我不认为该代码可以编译,您将copyList 用作指针(->next)和引用(.first)。最后的删除没有意义,copyList 应该始终为空。
  • 它确实可以编译,但它没有做它应该做的事情。

标签: c++ class operator-overloading linked-list


【解决方案1】:

你说“一些错误”。在提问时提及这些内容会很有帮助。

也就是说,看看copyAll 的前两行。第一行分配一个新节点,第二行覆盖该指针,永远丢失它。也许你的意思是copyList.first = l.first。您还需要在 while 循环内构造新节点。

【讨论】:

  • 在我的帖子中添加了更多信息。我不能像你现在所说的那样做 copyList.first。
  • 那么你需要this-&gt;first = copyList之类的东西,就在新声明之后。逐行检查你在纸上的代码,看看哪些指针指向哪里。还要考虑指针应该指向的位置,您的错误和解决方案将是显而易见的。
【解决方案2】:

基本上浅拷贝深拷贝的区别在于你是拷贝指针本身还是拷贝指针指向的所有数据。如果您的对象是派生的,不要忘记调用基类并复制其所有成员以避免部分初始化!

通常您希望为此提供一个复制构造函数。赋值运算符类似于复制,但赋值实际上是在已构造且可能已初始化的对象上完成的。

至于你是否做得好,这取决于你的类的实现细节,其中大部分你没有在这里展示。您显示的内容看起来不完整。

在尝试实现自己的版本之前,您可能需要考虑使用std::list,直到您更熟悉 C++ 和数据结构。除了出于好奇或更完整地学习基本概念之外,现在很少需要重新发明轮子。

【讨论】:

  • 这是为我的班级准备的,他们让我们为教育目的实施双向链表 XD。否则我肯定会使用 std::list!
猜你喜欢
  • 1970-01-01
  • 2016-01-29
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多