首页 文章

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

提问于
浏览
0

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

// --- 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();
        }
    }

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

  • 删除当前列表 .

  • 复制新列表 .

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

2 回答

  • 9

    对于复制赋值运算符,可以使用 copy-and-swap idiom .

    例如,在您的情况下,您可以将其添加到 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")
        }
    

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

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

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

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

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

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

    (当然,除了复制构造函数之外,还必须提供正确的析构函数定义(请参阅What is The Rule of Three?) . )

  • 1

    将所需的功能放在析构函数和复制构造函数以及赋值运算符调用的单独函数中 .

相关问题