c++ - 这是 C++ 链表的深层复制和正确实现的 = 运算符吗?

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

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

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:这是类(class)本身。我不允许添加任何其他成员函数:

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
 };

最佳答案

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

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

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

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

关于c++ - 这是 C++ 链表的深层复制和正确实现的 = 运算符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5721064/

相关文章:

python - 如何使用 OpenCV 绘制多色分段圆?

c++ - 如何在 Visual Studio C++ 2005 中处理所有#include 指令?

c++ - 重载+运算符

c++ - 如果 lhs 或 rhs 对象是 float ,则 operator+ 的结果是 float

java - 指向子类对象的父类(super class) ref 的类类型是什么?

c++ - 在同一个类中重载 operator< 和 operator>

c++ - 不同编译器输出不同

c++ - 如何如此快速地评估 const expr

java - 如何使用 OOP 构造一个实现循环算法的类?

c++ - 如何创建 C++ 用户定义的类,以便该类的 std::vector 不包含该类的某些成员