如何从复制赋值运算符调用复制构造函数?

She*_*lef 0 c++ destructor copy-constructor assignment-operator

我正在实施一个链表.我写了一个拷贝构造函数:

        // --- copy constructor ---
    IntList(const IntList& last_list) {
        first = new IntNode(last_list.getFirst()->getData());
        cout << "copy first " << first->getData() << endl;

        IntNode* last_node = first;
        IntNode* node = last_list.getFirst()->getNext();
        while(node!=NULL) {
            IntNode* new_node = new IntNode(node->getData());
            last_node->setNext(new_node);
            cout << "copy " << new_node->getData()<< endl;
            last_node = new_node;
            node = node->getNext();
        }
    }
Run Code Online (Sandbox Code Playgroud)

据我了解,我的副本赋值运算符(operator=)应该有2个目标:

  • 删除当前列表.
  • 复制新列表.

我可以通过调用我已编写的析构函数来实现这两个目标,然后调用复制构造函数.我该怎么做?

gx_*_*gx_ 9

对于您的复制赋值运算符,您可以使用复制和交换习惯用法.

例如,在您的情况下,您可以将其添加到您的IntList类定义中(给定您的复制构造函数的代码,我假设您唯一的数据成员是IntNode* first;):

        // --- swap non-member function ---
    friend void swap(IntList& a, IntList& b) /* no-fail */ {
        // swap data members one by one
        std::swap(a.first, b.first);
    }

        // --- (copy) assignment operator ---
    IntList& operator=(const IntList& other) {
        IntList temp(other); // copy construction
        swap(*this, temp);   // swap
        return *this;
                             // destruction of temp ("old *this")
    }
Run Code Online (Sandbox Code Playgroud)

实际上,这可能是更好的风格:

        // --- swap non-member function ---
    friend void swap(IntList& a, IntList& b) /* no-fail */ {
        using std::swap; // enable ADL
        // swap data members one by one
        swap(a.first, b.first);
    }

        // --- (copy) assignment operator ---
    IntList& operator=(IntList other) { // note: take by value
        swap(*this, other);
        return *this;
    }
Run Code Online (Sandbox Code Playgroud)

在C++ 98中,C++ 11中的/* no-fail */注释可以替换为throw()(或者只是/* throw() */注释)noexcept.

(注意:不要忘记预先包含正确的标题std::swap,即<algorithm>在C++ 98/03中,<utility>在C++ 11中(你也可以包括两者).)

备注:这里的swap函数是在类体中定义的,但它仍然是非成员函数,因为它是一个friend.

查看整个故事的链接帖子.

(当然,除了复制构造函数之外,还必须提供正确的析构函数定义(参见什么是三规则?).)